重定向在我已经在.htaccess文件中的以下规则:检查网站是否被的.htaccess
RewriteRule orders\.html orders.php [L,NC]
我如何检查orders.php用户是否进入orders.html或orders.php作为请求URL?
我想将用户重定向到orders.html,如果他/她在地址栏中有orders.php。
重定向在我已经在.htaccess文件中的以下规则:检查网站是否被的.htaccess
RewriteRule orders\.html orders.php [L,NC]
我如何检查orders.php用户是否进入orders.html或orders.php作为请求URL?
我想将用户重定向到orders.html,如果他/她在地址栏中有orders.php。
为了获得最初请求的URI从PHP重写后:
尝试$_SERVER['REDIRECT_URL']
。根据我的经验,Apache的mod_rewrite模块填充了CGI的REDIRECT_URL
变量。
$_SERVER['REQUEST_URI']
也应该是有用的。
做print_r($_SERVER);
看到在您的处置所有的变量。
REQUEST_URI就是我所需要的,谢谢!为什么不在phpinfo()中?我在写这篇文章之前看过那里。 – bpiec 2012-01-07 19:12:33
@bpiec:会的。 – 2012-01-07 19:19:19
我想重定向用户orders.html,如果他/她有orders.php在地址栏
如果你正在寻找一个.htaccess的解决方案,请尝试以下
代码RewriteEngine on
RewriteBase/
#if the user requested orders.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /orders\.php [NC]
#301 redirect them to orders.html
RewriteRule . orders.html [L,R=301]
#existing rule to rewrite .html to .php
RewriteRule ^orders\.html$ orders.php [L,NC]
为什么'THE_REQUEST'上的条件?好的方法,但... – 2012-01-07 10:32:09
@LightnessRacesinOrbit'THE_REQUEST'是唯一一个从原始请求中设置不变的变量,所以它不受后续重写影响 – 2012-01-07 14:02:12
有趣。你认为这可能是有用的解决http://stackoverflow.com/questions/5574442/why-does-this-cause-an-infinite-request-loop? – 2012-01-07 14:10:43
@Justin:来自浏览器的对'orders.php'的请求触发。不执行重写。 PHP脚本检测到'orders.php'被请求,并且(大概是用'header(“Location:...”)')要求浏览器重定向到'orders.html'。然后,来自浏览器的'orders.html'请求,触发重写规则,在内部重定向到'orders.php'(尽管用户仍然在URL栏中看到'orders.html')。我看不出在这里有无限重定向的可能性,而你的方式只会将用户重定向到一个不存在的非PHP页面。 – 2012-01-06 19:25:13