2011-02-27 140 views
2

我想創建一個移動Safari版本的瀏覽器嗅探器,問題是重定向創建一個無限循環,顯然,我想知道是否有辦法做到這一點:帶有mod_rewrite和重定向的iPad/iPhone瀏覽器嗅探器; 「太多的重定向」

http://mydomain.com(或任何其他requets_uri)[301] - >http://mydomain.com/ipad

下面是我使用的代碼片段:

RewriteCond %{REQUEST_URI} !^ipad #Trying to set the condition "if /ipad is not the request_uri, but it doesn't work 
RewriteCond %{HTTP_USER_AGENT} .*Mobile.*Safari 
RewriteRule ^(.*)$ ipad [R=301,L] 

更新:這是我的(修改)全套mod_rewrite的規則:

RewriteEngine On RewriteBase/

# iOS redirection. 
RewriteCond %{REQUEST_URI} !^/ipad 
RewriteCond %{HTTP_USER_AGENT} .*Mobile.*Safari 
RewriteRule ^(.*)$ /ipad [R=301,L] 

# If the requested URL does not exist (it's likely an agavi route), 
# pass it as path info to index.php, the Agavi dispatch script. 
RewriteRule ^$ index.php?/ [QSA,L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI}  !^/favicon.ico 
RewriteRule (.*) index.php?/$1 [QSA,L] 

更新2:FINAL;感謝的答覆,我是來這我張貼任何人有同樣的問題的一個解決方案:

RewriteEngine On 
RewriteBase/

# iOS redirection. 
# Make sure the URL hasn't already been rewritten internally 
RewriteCond %{ENV:REDIRECT_STATUS} ="" 
RewriteCond %{REQUEST_URI} !^/ios 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI}  !^/favicon.ico 
RewriteCond %{HTTP_USER_AGENT} .*Mobile.*Safari 
RewriteRule ^.*$ /ios [R,L] 

# If the requested URL does not exist (it's likely an agavi route), 
# pass it as path info to index.php, the Agavi dispatch script. 
RewriteRule ^$ index.php?/ [QSA,L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI}  !^/favicon.ico 
RewriteRule (.*) index.php?/$1 [QSA,L] 

回答

2

在你第一次的RewriteCond測試將總是成功,因爲%{REQUEST_URI}價值總是與開始領先的斜線,因此絕不可能只以「ipad」開頭。嘗試修改它,如下所示:

RewriteCond %{REQUEST_URI} !^/ipad 
RewriteCond %{HTTP_USER_AGENT} .*Mobile.*Safari 
RewriteRule ^(.*)$ /ipad [R=301,L] 

這也是一個好主意,把一個斜槓的「iPad」之前是考慮你的外部重定向,因爲我已經做了那裏。如果你設置了RewriteBase,這不會是個問題,但是你可能會遇到麻煩,否則由於mod_rewrite如何生成新的請求。

編輯:你與你的完整的規則集所面臨的問題,從事實%{REQUEST_URI}的價值將你的規則,第二塊之後改變,導致第一條件時,該規則集再次通過莖重新處理。這個後處理步驟是how mod_rewrite works in a per-directory context的一部分,所以你必須考慮它。幸運的是,這並不難:

RewriteEngine On 
RewriteBase/

# iOS redirection. 
# Make sure the URL hasn't already been rewritten internally 
RewriteCond %{ENV:REDIRECT_STATUS} ="" 
RewriteCond %{REQUEST_URI} !^/ipad 
RewriteCond %{HTTP_USER_AGENT} .*Mobile.*Safari 
RewriteRule ^(.*)$ /ipad [R=301,L] 

# If the requested URL does not exist (it's likely an agavi route), 
# pass it as path info to index.php, the Agavi dispatch script. 
RewriteRule ^$ index.php?/ [QSA,L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI}  !^/favicon.ico 
RewriteRule (.*) index.php?/$1 [QSA,L] 
+0

太棒了!謝謝你幫助Tim,非常感謝:) – lmerino 2011-02-27 16:48:35