简体   繁体   English

htaccess移动重定向到子目录

[英]htaccess mobile redirect to subdirectory

I've adapted a htaccess file that was provided here Mobile Redirect using htaccess however, what I'm trying to do is to check the user is not already in the subfolder(rather than sub domain) domain.co.uk/mobile and if not, redirect them to the mobile site. 我已经改编了htaccess此处提供的htaccess文件,该文件使用htaccess进行了移动重定向 ,但是我要做的是检查用户是否不在子文件夹(而不是子域)domain.co.uk/mobile中,如果否,请将其重定向到移动网站。

However, this just drops into an infinite loop and therefore fails. 但是,这只会陷入无限循环,因此会失败。

Can anyone tell me what the problem is and how I do this kind of RewriteCond? 谁能告诉我问题出在哪里以及我如何进行这种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]

You should change your mobile site check from !^/mobile/ to !^[/]?mobile . 您应该将移动站点检查从!^/mobile/更改为!^[/]?mobile Apache does not always include the leading slash in the REQUEST_URI . Apache并不总是在REQUEST_URI包含斜杠。 This regex makes that leading slash optional, so the rule will work whether the slash is present or not. 此正则表达式使该前导斜杠成为可选,因此无论该斜杠是否存在,该规则都将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM