Mobile redirection using .htaccess - only on homep

2019-09-01 09:55发布

I started by using the method described here in order to create a Mobile redirect, and it works perfectly.

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.

Example:


homepage URL = redirect


blog URL = NO-redirect

about us URL = NO-redirect

etc etc etc


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?

3条回答
Viruses.
2楼-- · 2019-09-01 10:43

The rewrite rules below were tested in an .htaccess file in the document root. Replace m.domain.com and domain.com as appropriate.

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]
查看更多
贼婆χ
3楼-- · 2019-09-01 10:47

For some freak reason, for now, this is working?!?!?

RewriteCond %{REQUEST_URI} (.*)/$

Why, I have no idea

查看更多
地球回转人心会变
4楼-- · 2019-09-01 10:52

Your problem is here:

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

The request uri cannot be both /index.php AND / at the same time. This regex needs to be combined into an either/or:

RewriteCond %{REQUEST_URI} ^/(index.php)?$ 
查看更多
登录 后发表回答