Redirect to a mobile version of your site with .htaccess

November 3, 2008 – 11:48 am

Sometimes it is better to create special pages fo mobile phone considering their smaller screens and lack of javascript and html compatibility. You can identify a mobile phone browser by checking its HTTP_ACCEPT var inside the $_SERVER var in php and then use header(”location: …”); to redirect te phones. However if you have a static html page that needs to be redirected and no javascript is expected to load on the phone, you have to use servers capabilities to redirect it. Here is the code that will do the trick:

RewriteEngine On

RewriteCond %{HTTP_ACCEPT} (x-)*(application|text)/(x-)*(vnd[-.])*(wap[-.]|wml)+
RewriteRule ^.*$ http://www.yourmobilesite.com [L]

It may need some tweaking and testing of the regular expression and I’m living it for you to modify it and post results here.

Instead of HTTP_ACCEPT you can use other server vars such as HTTP_USER_AGENT etc.

Post a Comment