Apache 2 mod_rewrite和PHP.从htaccess修改$ _SERVER ['REQUEST_URI']值? [英] Apache 2 mod_rewrite and PHP. Modify $_SERVER['REQUEST_URI'] value from htaccess?
问题描述
我有这个.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
,并且我希望从PHP打印$_SERVER['REQUEST_URI']
时会收到goodbye
.
So I'm receiving all the requests on index.php
, but I get hello
when asking for hello
, and I expected to receive goodbye
when printing $_SERVER['REQUEST_URI']
from PHP.
也就是说,即使匹配引用index.php
的RewriteRule之前已经重写了URL,$_SERVER['REQUEST_URI']
似乎也不可变.有什么办法可以修改这个值?
That is, $_SERVER['REQUEST_URI']
seems inmutable, even when the URL has been rewritten already before matching the RewriteRule referring index.php
. Is there any way to modify this value?
我想这样做是在不修改PHP文件的情况下,将URL预处理的薄薄而简单的一层添加到某些现有代码中.因此,我试图保留在.htaccess
内.
I want to do this to add a thin and simple layer of URL preprocessing to some existing code, without modifying the PHP files. So I'm trying to stick inside the .htaccess
.
推荐答案
首先,您犯了一个错误,没有在第一个规则中放置L
或PT
标志.您的代码应如下所示:
First of all you made a mistake of not putting L
or PT
flag in your first rule. Your code should be like this:
RewriteRule ^hello$ goodbye [PT]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
一旦有代码,就可以在index.php
中访问此变量:
Once that code is there access this variable in index.php
:
$_SERVER["REDIRECT_URL"]
这将具有值: /goodbye
This will have value: /goodbye
如果在主机上启用了mod_proxy
,则可以将第一个规则设置为:
If you have mod_proxy
enabled on your host, you can have your first rule as:
RewriteRule ^hello$ /goodbye [P]
然后您将拥有: $_SERVER["REQUEST_URI"]=/goodbye
And then you will have: $_SERVER["REQUEST_URI"]=/goodbye
这篇关于Apache 2 mod_rewrite和PHP.从htaccess修改$ _SERVER ['REQUEST_URI']值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!