.htaccess redirect to mobile and desktop in one file

Issue

I have simple code to check if user on my old site using mobile or not. If using then redirect to new site and mobile version with variable. But in the same time i want check if user are desktop and redirect to other site.

My code is:

RewriteEngine On

RewriteCond %{QUERY_STRING} (^|&)mobile=1(&|$)
RewriteRule ^ - [CO=mobile:1:%{HTTP_HOST}]
RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
RewriteRule ^ - [CO=mobile:0:%{HTTP_HOST}]
RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
RewriteRule ^ - [S=1]
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}       !^$
RewriteCond %{HTTP_HOST}          !^m\.
RewriteCond %{HTTP:Cookie}        !\mobile=0(;|$)
RewriteRule ^(.*)$ https://m.somesite.com/$1 [R=301,L]

And this is working, but where i can put special redirect with variable if user are desktop?

RewriteRule ^(.*)$ https://desktop.othersomesite.com/$1 [R=301,L]

something like this

<?php
...

if ($mobile = 1) {
  header('Location: https://m.somesite.com/$1');
} 
elseif ($mobile = 0) {
  header('Location: https://desktop.othersomesite.com/$1');;
}
?>

thanks 🙂

Solution

…where i can put special redirect with variable if user are desktop?

RewriteRule ^(.*)$ https://desktop.othersomesite.com/$1 [R=301,L]

It looks like you can just put it immediately after your existing directives (that redirects to the mobile site).

something like this

<?php

I’m not quite sure what you are trying to show here, other than perhaps trying to show the logic? But this would seem to be misleading since I don’t believe you want to implement this in PHP; do you? The PHP is also invalid (crucially using the assignment operator, instead of checking for equality).


However, some notes regarding your existing directives…

RewriteCond %{QUERY_STRING} (^|&)mobile=1(&|$)
RewriteRule ^ - [CO=mobile:1:%{HTTP_HOST}]
RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
RewriteRule ^ - [CO=mobile:0:%{HTTP_HOST}]
RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
RewriteRule ^ - [S=1]
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}       !^$
RewriteCond %{HTTP_HOST}          !^m\.
RewriteCond %{HTTP:Cookie}        !\mobile=0(;|$)
RewriteRule ^(.*)$ https://m.somesite.com/$1 [R=301,L]

The first two rules (that set the mobile cookie) can be combined into one. The third rule can be converted into a single RewriteCond directive on the main rule that follows.

There is a HTTP_COOKIE server variable, so I would tend to use this instead of HTTP:Cookie (to access the header directly). The \m in the last CondPattern looks like an error – although this still matches a literal m, so it "works". However, this also matches any cookie whose name ends with "mobile", not just mobile. The regex needs to something like (^|;|\s)mobile=0(;|$) instead, since cookie name=value pairs are often delimited by ;<space>.

So, bringing these points together we have:

# Set mobile cookie if mobile present in query string
RewriteCond %{QUERY_STRING} (?:^|&)mobile=(0|1)(?:&|$)
RewriteRule ^ - [CO=mobile:%1:%{HTTP_HOST}]

# Mobile
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}       !^$
RewriteCond %{HTTP_HOST}          !^m\.
RewriteCond %{QUERY_STRING}       !(^|&)mobile=0(&|$)
RewriteCond %{HTTP_COOKIE}        !(^|;|\s)mobile=0(;|$)
RewriteRule (.*) https://m.somesite.com/$1 [R=301,L]

# Else Desktop
RewriteRule (.*) https://desktop.othersomesite.com/$1 [R=301,L]

Also, since you are potentially basing the redirect on the User-Agent and Cookie headers, these should also be added to the Vary header to ensure proper caching from any intermediaries. For example, add the following after the above:

Header always merge Vary "User-Agent, Cookie"

Answered By – MrWhite

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published