iPad / iPhone的浏览器嗅探器与mod_rewrite的和重定向; "太多的重定向"重定向、太多、浏览器、iPhone

2023-09-02 00:36:59 作者:刀枪不入i

我想创建一个浏览器嗅探器的移动Safari浏览器的版本,问​​题是,重定向创建一个无限循环,很明显,我不知道是否有一种方法可以做到这一点:

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

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

 的RewriteCond%{REQUEST_URI}!^ iPad的#Trying设定的条件,如果/ iPad是不是REQUEST_URI,但它不工作
的RewriteCond%{HTTP_USER_AGENT} *移动* Safari浏览器
重写规则^(。*)$的ipad [R = 301,L]
 

更新:这是我的(修改)全套mod_rewrite规则:

  RewriteEngine叙述上的RewriteBase /

#iOS的重定向。
的RewriteCond%{REQUEST_URI}!^ / iPad的
的RewriteCond%{HTTP_USER_AGENT} *移动* Safari浏览器
重写规则^(。*)$ / iPad的[R = 301,L]

#如果请求的URL不存在(这是很可能的Agavi的路线),
#把它作为路径信息到index.php,在Agavi的调度脚本。
重写规则^ $的index.php?/ [QSA,L]
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{REQUEST_URI}!^ /的favicon.ico
重写规则(。*)的index.php?/ $ 1 [QSA,L]
 
ipad air 如何下载360安全浏览器

更新2 :决赛;感谢答复,我来下一个解决方案,我张贴任何人有同样的问题:

  RewriteEngine叙述上
的RewriteBase /

#iOS的重定向。
#确保网址尚未内部重写
的RewriteCond%{ENV:REDIRECT_STATUS} =
的RewriteCond%{REQUEST_URI}!^ / IOS
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{REQUEST_URI}!^ /的favicon.ico
的RewriteCond%{HTTP_USER_AGENT} *移动* Safari浏览器
重写规则^ * $ / IOS [R,L]

#如果请求的URL不存在(这是很可能的Agavi的路线),
#把它作为路径信息到index.php,在Agavi的调度脚本。
重写规则^ $的index.php?/ [QSA,L]
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{REQUEST_URI}!^ /的favicon.ico
重写规则(。*)的index.php?/ $ 1 [QSA,L]
 

解决方案

在你第一次的RewriteCond测试将永远成功,因为值%{REQUEST_URI} 总是以领先的斜线,因此也从不以刚的iPad。尝试修改它,如下所示:

 的RewriteCond%{REQUEST_URI}!^ / iPad的
的RewriteCond%{HTTP_USER_AGENT} *移动* Safari浏览器
重写规则^(。*)$ / iPad的[R = 301,L]
 

这也是一个不错的主意,把正斜杠的iPad之前是考虑外部重定向,因为我已经做了那里。如果你已经设置的RewriteBase,这不会是一个问题,但你可能会遇到麻烦,否则由于如何mod_rewrite的产生了新的要求。

修改:你面对你的完整的规则集的问题来源于这样的事实说的%{REQUEST_URI}值将改变的规则的第二块之后,使所述第一状态到再次通过当规则集重新处理。这种后处理步骤是how mod_rewrite的工作在每个目录范围内的,所以你必须把它考虑进去。幸运的是,这并不难做到:

  RewriteEngine叙述上
的RewriteBase /

#iOS的重定向。
#确保网址尚未内部重写
的RewriteCond%{ENV:REDIRECT_STATUS} =
的RewriteCond%{REQUEST_URI}!^ / iPad的
的RewriteCond%{HTTP_USER_AGENT} *移动* Safari浏览器
重写规则^(。*)$ / iPad的[R = 301,L]

#如果请求的URL不存在(这是很可能的Agavi的路线),
#把它作为路径信息到index.php,在Agavi的调度脚本。
重写规则^ $的index.php?/ [QSA,L]
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{REQUEST_URI}!^ /的favicon.ico
重写规则(。*)的index.php?/ $ 1 [QSA,L]
 

I'm trying to create a browser sniffer for Mobile Safari version, the problem is that the redirection creates an infinite loop, obviously, and I'm wondering if there's a way to do this:

http://mydomain.com (or any other requets_uri) [301]-> http://mydomain.com/ipad

Here's the snippet I'm using:

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]

Update: Here is my (modified) full set of mod_rewrite rules:

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]

Update 2: FINAL; Thanks to the replies, I've come to the next solution which I'm posting for anybody having the same issues:

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]

解决方案

The test in your first RewriteCond will always succeed because the value of %{REQUEST_URI} always begins with a leading forward slash, and therefore can never begin with just "ipad". Try modifying it as follows:

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

It's also a good idea to put a forward slash before "ipad" on account of your external redirection, as I've done there. If you've set RewriteBase, this won't be an issue, but you'll probably run into trouble otherwise due to how mod_rewrite generates the new request.

Edit: The problem you're facing with your complete rule set stems from the fact that the value of the %{REQUEST_URI} will change after your second block of rules, causing the first condition to pass again when the rule set is reprocessed. This reprocessing step is part of how mod_rewrite works in a per-directory context, so you have to take it into consideration. Luckily, that's not hard to do:

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]