问题描述
我开始使用方法描述,以创造一个移动的重定向这里和它完美的作品。
I started by using the method described here in order to create a Mobile redirect, and it works perfectly.
我下一步需要做,但是,发生的任何页面比其他网页上的prevent它。换句话说:如果用户通过移动设备加载网页,重定向应该发生 - 但如果他们在移动设备上加载任何其他页面,不应该发生的重定向
What I need to do next, however, is prevent it from happening on any page other than the homepage. In other words: If the user loads the homepage from a mobile device, the redirect should happen - but if they load any other page from a mobile device, the redirect should not occur.
例如:
主页URL =重定向
博客URL = NO-重定向
blog URL = NO-redirect
关于我们URL = NO-重定向
about us URL = NO-redirect
等等等等
下面是code,我有当前:
Here is the code that I have currently:
# 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]
和没有工作。我该怎么办?
And that didn't work. What do I do?
推荐答案
下面是在文档根目录的的.htaccess
文件测试的重写规则。替换 m.domain.com
和 domain.com
合适。
The rewrite rules below were tested in an .htaccess
file in the document root. Replace m.domain.com
and domain.com
as appropriate.
此外,你可以通过附加强制移动的任何URL或关闭?M = 0
或?M = 1
,或设定的脚本语言(PHP在这种情况下)。
Additionally you can force mobile on or off for any URL by appending ?m=0
or ?m=1
, or setting the mredir
cookie with the scripting language (php in this case).
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]
这篇关于使用的.htaccess移动重定向 - 仅在主页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!