Apache2Mod_重写和PHP。从htaccess修改$服务器[';请求URI';]值?
我有一个Apache2Mod_重写和PHP。从htaccess修改$服务器[';请求URI';]值?,php,regex,apache,.htaccess,mod-rewrite,Php,Regex,Apache,.htaccess,Mod Rewrite,我有一个.htaccess文件: RewriteEngine On RewriteRule ^hello$ goodbye RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !=/favicon.ico RewriteRule ^ index.php [L] 因此,我在index.php上接收所有请求,但我在请求hello时收到hello,我希
.htaccess
文件:
RewriteEngine On
RewriteRule ^hello$ goodbye
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
因此,我在index.php
上接收所有请求,但我在请求hello
时收到hello
,我希望在打印$\u服务器['REQUEST\u URI']
时从php收到再见
也就是说,$\u服务器['REQUEST\u URI']
似乎是不可变的,即使URL在匹配参考index.php的重写规则之前已经被重写了。有没有办法修改这个值
我想这样做是为了在一些现有代码中添加一个精简而简单的URL预处理层,而无需修改PHP文件。因此,我试图坚持.htaccess
首先,您犯了一个错误,没有将L
或PT
标记放在第一条规则中。您的代码应该如下所示:
RewriteRule ^hello$ goodbye [PT]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
一旦有了该代码,就可以在index.php
中访问该变量:
$_SERVER["REDIRECT_URL"]
这将具有以下值:/bye
编辑
如果您在主机上启用了mod_proxy
,则可以将第一条规则设置为:
RewriteRule ^hello$ /goodbye [P]
然后您将有:$\u SERVER[“REQUEST\u URI”]=/bye
是的,常用的方法是使用[E=]设置一个新的环境变量