我已经为我的网站定义了一个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,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,因为它尚未处理.
原文链接:https://www.f2er.com/php/131614.htmlThe 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