regex - .htaccess 将 301 重定向到移动目录 - 重定向错误太多

标签 regex apache .htaccess mod-rewrite mobile

嘿,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/

相关文章:

php - 使用 codeigniter 和 htaccess 删除 index.php 和 301 重定向

javascript - 将根域重定向到子域的影响

php - 如何使用带有 'e' 修饰符的 preg_replace() 传递参数?

regex - 使用 Scala 中的 Spark 使用 Regex 过滤 DataFrame

android - Android 上的简单 Web 服务器

apache - 在 Apache Solr 中设置默认搜索字段

php - 为 seo 优化和 google 引用重写 url

ruby - 排除某些单词的正则表达式

ruby - 如何在 Ruby 正则表达式中使用 AND

java - tomcat 8 在服务器上运行时出现问题,如磁盘空间不足