0

我首先使用此处描述的方法来创建移动重定向,并且效果很好。

然而,我接下来需要做的是防止它发生在主页以外的任何页面上。换句话说:如果用户从移动设备加载主页,则应该发生重定向 - 但如果他们从移动设备加载任何其他页面,则不应发生重定向。

例子:


主页 URL = 重定向


博客 URL = 不重定向

关于我们 URL = NO-redirect

等等等等


这是我目前拥有的代码:

# 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.com]

# Failed attempt at redirecting only homepage
RewriteCond %{HTTP_HOST}  ^(domain\.com|www\.domain\.com)$ [NC]
# Check if we're just going to the homepage - fail
RewriteCond %{REQUEST_URI} ^/(index.php)?$ 

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 %{HTTP_HOST}          !^m\.

# 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 ^(index\.php)?$ http://m.domain.com [R,L]

那没有用。我该怎么办?

4

3 回答 3

1

下面的重写规则在.htaccess文档根目录中的一个文件中进行了测试。酌情更换m.domain.comdomain.com

?m=0此外,您可以通过附加或或使用脚本语言(在本例中为 php)?m=1设置cookie来强制打开或关闭任何 URL 的移动设备。mredir

RewriteEngine On

# Put forced mobile state into 'mredir' cookie
RewriteCond %{QUERY_STRING} (?:^|&)m=([0-1])(?:&|$)
RewriteRule ^ - [CO=mredir:%1:.domain.com]

# Forced mobile? (m=1)
RewriteCond %{HTTP_HOST}    !^m\.domain\.com$
RewriteCond %{QUERY_STRING} (^|&)m=1(&|$) [OR]
RewriteCond %{HTTP_COOKIE}  ^.*mredir=1
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$)
RewriteRule ^               http://m.domain.com [R=302,L]

# Forced regular? (m=0)
RewriteCond %{HTTP_HOST}    ^m\.domain\.com$
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$) [OR]
RewriteCond %{HTTP_COOKIE}  ^.*mredir=0
RewriteCond %{QUERY_STRING} !(^|&)m=1(&|$)
RewriteRule ^               http://domain.com [R=302,L]

# Auto redirect to mobile if not forced above
RewriteCond %{HTTP_HOST} !^m\.domain\.com$
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$)
RewriteCond %{HTTP_COOKIE} !.*mredir=0
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]
RewriteCond %{HTTP:x-wap-profile}%{HTTP:Profile} !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-|dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-|maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv|palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany|sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo|teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi|wapp|wapr|webc|winw|winw|xda|xda-|up.browser|up.link|windowssce|iemobile|mini|mmp|symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC]
# Only auto-redirect on / or /index.php
RewriteRule ^(\/|index\.php)?$ http://m.domain.com [R=302,L]
于 2013-06-26T16:35:17.997 回答
0

你的问题在这里:

RewriteCond %{REQUEST_URI} ^/index.php$ 
RewriteCond %{REQUEST_URI} ^/$

请求 uri 不能同时为/index.php AND /。这个正则表达式需要组合成一个/或:

RewriteCond %{REQUEST_URI} ^/(index.php)?$ 
于 2013-06-21T20:15:44.010 回答
0

出于某种奇怪的原因,现在,这是有效的?!?!?

RewriteCond %{REQUEST_URI} (.*)/$

为什么,我不知道

于 2013-06-25T20:53:35.323 回答