使用htaccess重定向到移动网站

时间:2016-09-20 21:06:45

标签: .htaccess redirect mobile

如何将用户重定向到我的移动页面?

我在htaccess上使用此代码

RewriteEngine on
RewriteCond %{QUERY_STRING} !^desktop
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-mobile|iemobile|iphone|ipod|#opera mobile|palmos|webos" [NC]
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]
RewriteRule ^$ http://example.com/mobile [L,R=302]

只有当我访问site.com时,它才能将我重定向到http://example.com/mobile 但我希望它就像是,即使我转到http://example.com/page1.php或任何其他网页,它也应该将我重定向到所有移动用户的http://example.com/mobile

请帮我解决这个问题

1 个答案:

答案 0 :(得分:1)

好吧,只需改变

RewriteRule ^$ http://example.com/mobile [L,R=302]

RewriteRule ^/?(.*)$ http://example.com/mobile [L,R=302]

或者

RewriteRule ^/?(.*)$ http://example.com/mobile/$1 [L,R=302,QSA]

要重定向到其他主机名(有时称为“子域名”),如下面的评论所示,您可以这样做:

RewriteRule ^/?(.*)$ http://m.example.com/$1 [L,R=302,QSA]

一般性评论:我认为http-301对于这种情况确实比http-302更有意义......