JGuff330
JGuff330

Reputation: 13

I have tried for days to get the right htaccess mobile redirect coding

I have tested out hundreds of different codes, either they work and screw something else up, or they don't work at all. This sounds lazy but I really have tried hundreds of combinations.

I have my main domain that I want redirected to my "mobile" sub domain (detecting mobile agent), and along with a folder of .html pages I have. Both directories match up file name wise.

Any ideas for me?

Upvotes: 1

Views: 92

Answers (1)

Alex W
Alex W

Reputation: 38173

Here is a website with information on how to redirect based on the browser's user-agent string:

http://www.howtoforge.com/apache2-how-to-redirect-users-to-mobile-or-normal-web-site-based-on-device-using-mod_rewrite

Your .htaccess should look like so:

RewriteEngine On

# Check if this is the noredirect query string
RewriteCond %{QUERY_STRING} (^|&)noredirect=true(&|$)
# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:%{HTTP_HOST},S]

# Check if this looks like a mobile device
# (You could add another [OR] to the second one and add in what you
#  had to check, but I believe most mobile devices should send at
#  least one of these headers)
RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP:Profile}       !^$
# Check if we're not already on the mobile site
RewriteCond %{HTTP_HOST}          !^mobile\.
# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP:Cookie}        !\smredir=0(;|$)
# Now redirect to the mobile site
RewriteRule ^ http://mobile.jguffphotography.com/%{REQUEST_URI} [R,L]

Make sure you update to the latest code above.

I have updated all of the code above to reflect what is should look like.

Upvotes: 1

Related Questions