gpt4 book ai didi

php - 如何在 .htaccess 文件中定义变量并使用它?

转载 作者:行者123 更新时间:2023-12-03 00:11:15 24 4
gpt4 key购买 nike

我已经为我的网站定义了一个 htaccess 文件,其中包含以下代码:

RewriteEngine on
RewriteBase /

RewriteRule ^bit_auth\/?(.*)$ /cig/base3/auth/$1 [R=301,NC,L]
RewriteCond $1 !^(index\.php|images|robots\.txt|assets|themes|includes)
RewriteRule ^(.*)$ /cig/base3/index.php/$1 [L]

我想将我的网站根路径保留在变量中,如下所示:

RewriteEngine on
RewriteBase /
SetEnv BASE_PATH "/cig/base3"

RewriteRule ^bit_auth\/?(.*)$ %{ENV:BASE_PATH}/auth/$1 [R=301,NC,L]
RewriteCond $1 !^(index\.php|images|robots\.txt|assets|themes|includes)
RewriteRule ^(.*)$ %{ENV:BASE_PATH}/index.php/$1 [L]

因为我以后可能需要在更多代码中使用BASE_PATH值,而且它也可能会被更改,我不想每次都在我的htaccess文件中搜索和替换。但是当我使用上面的代码时,在 htaccess 文件 %{ENV:BASE_PATH} 返回一个空值,而不是预期的 /cig/base3 但是在 php 中,当我调用它时使用:

<?php $specialPath = getenv('BASE_PATH'); var_dump($specialPath)?>

它显示了 /cig/base3 的正确值。

我的代码有什么问题以及如何解决这个问题?

最佳答案

您不能以这种方式使用 SetEnv,因为它尚未处理。

The internal environment variables set by this directive are set after most early request processing directives are run, such as access control and URI-to-filename mapping. If the environment variable you're setting is meant as input into this early phase of processing such as the RewriteRule directive, you should instead set the environment variable with SetEnvIf.

您正在寻找的是setenvif而不是SetEnv

所以你应该能够做类似的事情

SetEnvIf Request_URI "^.*" base_path=/cig/base3

http://httpd.apache.org/docs/2.2/mod/mod_setenvif.html#setenvif

关于php - 如何在 .htaccess 文件中定义变量并使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30399109/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com