我正在尝试为Mobile Safari版本创建浏览器嗅探器,问题是重定向会创建一个无限循环,显然,我想知道是否有办法执行此操作:
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 :最终;感谢回复,我来到下一个解决方案,我发布给任何有相同问题的人:
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]
答案 0 :(得分: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]