Apache 2 mod_rewrite和PHP.从htaccess修改$ _SERVER ['REQUEST_URI']值? [英] Apache 2 mod_rewrite and PHP. Modify $_SERVER['REQUEST_URI'] value from htaccess?

查看:100
本文介绍了Apache 2 mod_rewrite和PHP.从htaccess修改$ _SERVER ['REQUEST_URI']值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个.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.

推荐答案

首先,您犯了一个错误,没有在第一个规则中放置LPT标志.您的代码应如下所示:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆