Web design, programming, graphics, and pretty much anything else I care about.

Apache redirect, with cookie support

This will redirect visitors that come from various mobile devices to another website (a mobile site.) It has added support for a cookie, so that mobile users can still opt to visit the full site.

The first block checks to see if it is a mobile device and there is neither a cookie nor a url parameter set. If those conditions are met, redirect the visitor to the mobile site. The second block looks to see if the url parameter is set and there is no cookie. If those conditions are met, set a cookie for the user.

In practice, this will automatically redirect mobile users to a mobile site. On the mobile site, add a link going to www.example.com?nomobile=true. When the visitor uses that link, they will get back to the original site, a cookie will be set, and they can now bypass the redirect while the cookie is still valid. The point is to redirect mobile visitors, but not permanently if they still want to visit the full version of the site.

# Mobile redirect
RewriteEngine on
RewriteCond %{SERVER_NAME} ^www.example.com [NC]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-mobile|iemobile|iphone|ipod|opera mobile|palmos|webos" [NC]
RewriteCond %{QUERY_STRING} !^nomobile=true$ [NC]
RewriteCond %{HTTP_COOKIE} !nomobile=true [NC]
RewriteRule ^/$ http://m.example.com? [R]

# Set cookie, if user doesn't want to be redirected
RewriteCond %{QUERY_STRING} ^nomobile=true$
RewriteCond %{HTTP_COOKIE} !^.*nomobile.*$ [NC]
RewriteRule .* - [co=nomobile:true:.example.com:7200:/]

Lets break it down.

Turn the rewrite engine on.

RewriteEngine on

Check if the requested server name begins with www.example.com. NC means not case-sensitive.

RewriteCond %{SERVER_NAME} ^www.example.com [NC]

Check if the user agent report by the device is in the following list.

RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-mobile|iemobile|iphone|ipod|opera mobile|palmos|webos" [NC]

Check if there is NOT a url parameter called "nomobile" with a value of "true". This value and parameter can be anything. I chose this, as it made sense to me.

RewriteCond %{QUERY_STRING} !^nomobile=true$ [NC]

Check if there is NOT a cookie called "nomobile" with a value of "true".

RewriteCond %{HTTP_COOKIE} !nomobile=true [NC]

If all the above conditions are met, rewrite and send off to the mobile site. "?" means remove any additional url parameters, and R means force an external redirect.

RewriteRule ^/$ http://m.example.com? [R]

Check if there IS a url parameter called "nomobile" with a value of "true".

RewriteCond %{QUERY_STRING} ^nomobile=true$

Check if there is NOT a cookie called "nomobile".

RewriteCond %{HTTP_COOKIE} !^.*nomobile.*$ [NC]

If the above conditions are met use a rewrite rule to leave the url the same, but use Apache to add a cookie. The cookie will be called "nomobile" and its value with be "true". It is available to the whole example.com domain (note the period before example.com.) The cookie will last for 7200 minutes.

RewriteRule .* - [co=nomobile:true:.example.com:7200:/]