2015-04-01 49 views
0

我要打一次工作是什麼:重定向直接文件訪問或重寫無需進一步重寫或重定向

用戶請求與/command/rewritehandler.php開始,被重定向到/

或用戶的任何請求需要別的如果用戶請求fullpath,則不會以/command/開頭,並且這將轉換/command/rewritehandler.php?q=fullpath

RewriteEngine On 
#Next two lines works with redirect 
RewriteCond %{REQUEST_URI} ^/command/rewritehandler\.php 
RewriteRule .* /? [L,R] 
#Next two lines does expected rewritting without above two lines 
RewriteCond %{REQUEST_URI} (.+) 
RewriteRule !^command/ /command/rewritehandler.php?q=%1 [B,L] 

但是在預期重寫後,它被重定向到/。這怎麼可以避免? L標記不會阻止其他RewriteRule在這裏運行。背景:How to bypass %23 (hash sign) through RewriteRule without data loss?

回答

2

使用THE_REQUEST變量在第一條規則:

RewriteEngine On 

#Next two lines works with redirect 
RewriteCond %{THE_REQUEST} \s/+command/rewritehandler\.php [NC] 
RewriteRule .* /? [L,R] 

#Next two lines does expected rewritting without above two lines 
RewriteCond %{REQUEST_URI} (.+) 
RewriteRule !^command/ /command/rewritehandler.php?q=%1 [B,L] 

THE_REQUEST變量代表從您的瀏覽器Apache接收到原始請求,並沒有得到一些重寫規則執行後覆蓋。此變量的示例值爲GET /index.php?id=123 HTTP/1.1

+1

如果瀏覽器發送//command///rewritehandler.php,則這不起作用。 '\ s/+ command/+ rewritehandler \ .php'也可以解決這個多重斜槓問題。 – BalticMusicFan 2015-04-01 22:37:28