嘿,StackOverflow 社区,我在 stackoverflow 上搜索了很多以修复我的“重定向过多”错误。 但找不到任何有帮助的东西。我想通过 .htaccess 和 UserAgent 条件重定向到移动目录。找到了一个高票数的解决方案,如下所示:
RewriteEngine On RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|ipad|iemobile" [NC] RewriteRule ^(.*)$ http://mysite.com/mobile/$1 [R=301,L]
重定向本身效果很好,但似乎移动浏览器在循环中添加/mobile,因此 URL 会变成类似 http://mysite.com/mobile/mobile/mobile/mobile/mobile/mobile/mobile/mobile 的内容。直到我收到错误“由于重定向太多而无法打开网站”。
我完整的 .htaccess 如下所示:
AddType video/ogg .ogv AddType video/mp4 .mp4 AddType video/webm .webm RewriteEngine On RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|ipad|iemobile" [NC] RewriteRule ^(.*)$ http://mysite.com/mobile/$1 [R=301,L]
预先感谢卢卡斯·蒂托
最佳答案
将您的 RewriteRule
替换为:
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|ipad|iemobile" [NC]
RewriteRule ^((?!mobile/).*)$ /mobile/$1 [R=301,L]
问题是您无条件地在任何网址之前添加 /mobile/
前缀,即使是那些已经以 /mobile/
开头的网址。
关于regex - .htaccess 将 301 重定向到移动目录 - 重定向错误太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19009585/