Search code examples
regex.htaccessmod-rewritemobileuser-agent

Redirect desktop users away from mobile site with .htaccess


Hello Folks at Stackoverflow,

I'm looking to redirect desktop users away from mobile site with .htaccess:

I already have this code which works perfectly when a mobile user tries to access the mobile version of my website example: m.website.com.

RewriteEngine On
RewriteCond %{QUERY_STRING} !^desktop
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-mobile|iemobile|iphone|ipod|#opera mobile|palmos|webos" [NC]
RewriteRule ^$ http://m.website.com [L,R=302] 

However if a user types m.website.com on a desktop browser, it goes directly to the mobile version content.

Is there a way to add any additional code to the .htaccess file to make it work in a way that when a desktop user attempts to go to m.website.com it remains in the desktop version.


Solution

  • Place this additional rule in .htaccess of DOCUMENT_ROOT of m.website.com:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^m\. [NC]
    RewriteCond %{HTTP_USER_AGENT} !(android|blackberry|googlebot-mobile|iemobile|iphone|ipod|opera\smobile|palmos|webos) [NC]
    RewriteRule ^ http://website.com%{REQUEST_URI} [L,R=302]