Ballu Rocks
Ballu Rocks

Reputation: 1226

Redirect website to Mobile version website using htaccess

HI i have used the following code to redirect to mobile website.It works fine from mobile to mobile website, domain.com to m.domain.com in mobile, but "?id=9" at the end of url is coming . How to remove the

 ?id=9 

from the url .And how to redirect the mobile webiste m.domain.com from desktop browser to domain.com . The below code only redirect the website from mobile to mobile webiste 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]

Upvotes: 2

Views: 4082

Answers (1)

anubhava
anubhava

Reputation: 786091

To strip query string append ? in the target URL like this:

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

Upvotes: 3

Related Questions