【问题标题】:htaccess mobile redirect to subdirectoryhtaccess 移动重定向到子目录
【发布时间】:2012-10-22 13:18:06
【问题描述】:

我已经调整了此处提供的 htaccess 文件 Mobile Redirect using htaccess 但是,我要做的是检查用户是否已经在子文件夹(而不是子域)中 domain.co.uk/mobile如果没有,请将它们重定向到移动网站。

但是,这只是陷入无限循环,因此失败。

谁能告诉我问题是什么以及我是如何做这种 RewriteCond 的?

RewriteEngine on
RewriteBase /

# Check if this is the noredirect query string
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:www.domain.co.uk]

RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP:Profile}       !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT}  "maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "wapp|wapr|webc|winw|winw|xda|xda-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "up.browser|up.link|windowssce|iemobile|mini|mmp" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC]
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]

# Check if we're not already on the mobile site
RewriteCond %{REQUEST_URI} !^/mobile/ [NC]
# Can not read and write cookie in same request, must duplicate condition
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$) 

# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP_COOKIE} !^.*mredir=0.*$ [NC]

# Now redirect to the mobile site
RewriteRule ^ http://www.domain.co.uk/mobile [R,L]

【问题讨论】:

  • 您可以尝试将您的移动站点检查从 !^/mobile/ 更改为 !^[/]?mobile - 根据我过去阅读的内容,有时前导 / 不在 REQUEST_URI 中。另外,您可以尝试启用重写日志。
  • 瑞恩,这似乎成功了!非常感谢你。如果你想把它作为答案,我会做更多的测试然后接受?
  • 完成。之前没有把它作为答案,因为这只是一个猜测。 :)

标签: html apache .htaccess redirect hosting


【解决方案1】:

您应该将移动站点检查从 !^/mobile/ 更改为 !^[/]?mobile。 Apache 并不总是在 REQUEST_URI 中包含前导斜杠。此正则表达式使前导斜杠成为可选的,因此无论斜杠是否存在,该规则都将起作用。

【讨论】:

  • 酷,再次感谢。将在其他手机上进行更多测试,然后接受。到目前为止看起来不错:)
猜你喜欢
  • 2014-02-23
  • 2019-02-19
  • 1970-01-01
  • 1970-01-01
  • 2015-10-08
  • 1970-01-01
  • 2013-10-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多