使用htaccess将网站重定向到移动版网站

时间:2013-06-08 14:23:22

标签: .htaccess redirect mobile

我已经使用以下代码重定向到移动网站。从移动设备到移动网站,domain.com到移动设备中的m.domain.com都可以正常工作,但网址末尾的“?id = 9”是来了。如何删除

 ?id=9 
来自网址的

。以及如何将移动网站m.domain.com从桌面浏览器重定向到domain.com。以下代码仅将网站从移动设备重定向到移动网站m.domain.com

# Check if mobile=1 is set and set cookie 'mobile' equal to 1
RewriteCond %{QUERY_STRING} (^|&)mobile=1(&|$)
RewriteRule ^ - [CO=mobile:1:%{HTTP_HOST}]

# Check if mobile=0 is set and set cookie 'mobile' equal to 0
RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
RewriteRule ^ - [CO=mobile:0:%{HTTP_HOST}]

# cookie can't be set and read in the same request so check
RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
RewriteRule ^ - [S=1]

# Check if this looks like a mobile device
RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera  mobile|palmos|webos|googlebot-mobile" [NC,OR]
RewriteCond %{HTTP:Profile}       !^$

# Check if we're not already on the mobile site
RewriteCond %{HTTP_HOST}          !^m\.
# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP:Cookie}        !\mobile=0(;|$)
# Now redirect to the mobile site
RewriteRule ^ http://m.example.com%{REQUEST_URI} [R,L]

1 个答案:

答案 0 :(得分:1)

要剥离查询字符串,请在目标网址中附加?,如下所示:

RewriteRule ^ http://m.example.com%{REQUEST_URI}? [R,L]