2012-01-06 72 views
1

重定向在我已經在.htaccess文件中的以下規則:檢查網站是否被的.htaccess

RewriteRule orders\.html orders.php [L,NC] 

我如何檢查orders.php用戶是否進入orders.html或orders.php作爲請求URL?

我想將用戶重定向到orders.html,如果他/她在地址欄中有orders.php。

+0

@Justin:來自瀏覽器的對'orders.php'的請求觸發。不執行重寫。 PHP腳本檢測到'orders.php'被請求,並且(大概是用'header(「Location:...」)')要求瀏覽器重定向到'orders.html'。然後,來自瀏覽器的'orders.html'請求,觸發重寫規則,在內部重定向到'orders.php'(儘管用戶仍然在URL欄中看到'orders.html')。我看不出在這裏有無限重定向的可能性,而你的方式只會將用戶重定向到一個不存在的非PHP頁面。 – 2012-01-06 19:25:13

回答

1

爲了獲得最初請求的URI從PHP重寫後:

  1. 嘗試$_SERVER['REDIRECT_URL']。根據我的經驗,Apache的mod_rewrite模塊填充了CGI的REDIRECT_URL變量。

  2. $_SERVER['REQUEST_URI']也應該是有用的。

print_r($_SERVER);看到在您的處置所有的變量。

+0

REQUEST_URI就是我所需要的,謝謝!爲什麼不在phpinfo()中?我在寫這篇文章之前看過那裏。 – bpiec 2012-01-07 19:12:33

+0

@bpiec:會的。 – 2012-01-07 19:19:19

1

我想重定向用戶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] 
+0

爲什麼'THE_REQUEST'上的條件?好的方法,但... – 2012-01-07 10:32:09

+0

@LightnessRacesinOrbit'THE_REQUEST'是唯一一個從原始請求中設置不變的變量,所以它不受後續重寫影響 – 2012-01-07 14:02:12

+0

有趣。你認爲這可能是有用的解決http://stackoverflow.com/questions/5574442/why-does-this-cause-an-infinite-request-loop? – 2012-01-07 14:10:43