Mobile Redirect using htaccess
I tested bits and pieces of the following, but not the complete rule set in its entirety, so if you run into trouble with it let me know and I'll dig around a bit more. However, assuming I got everything correct, you could try something like the following:
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} !^m\.
# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP:Cookie} !\smredir=0(;|$)
# Now redirect to the mobile site
RewriteRule ^ http://m.example.org%{REQUEST_URI} [R,L]
how to redirect from mobile site to full site with .htaccess
Create your link as: http://m.domain.com/some-link?mobile=0
Full .htaccess:
RewriteEngine On
RewriteBase /
#http://stackoverflow.com/questions/3680463/mobile-redirect-using-htaccess
# Check if mobile=1 is set and set cookie 'mobile' equal to 1
RewriteCond %{QUERY_STRING} (^|&)mobile=1(&|$)
RewriteRule ^ - [CO=mobile:1:%{HTTP_HOST}]
# Check if mobile=0 is set and set cookie 'mobile' equal to 0
RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
RewriteRule ^ - [CO=mobile:0:%{HTTP_HOST}]
# cookie can't be set and read in the same request so check
RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
RewriteRule ^ - [S=1]
# Check if this looks like a mobile device
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 %{QUERY_STRING} !^mobile=0(?:&|$)
# Check if we're not already on the mobile site
RewriteCond %{HTTP_HOST} !^m\.
# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP:Cookie} !\mobile=0(;|$)
# Now redirect to the mobile site
RewriteRule ^ http://m.domain.com%{REQUEST_URI} [R,L]
# Go back to full site
RewriteCond %{HTTP_HOST} ^m\.
RewriteCond %{QUERY_STRING} (?:^|&)mobile=0(?:&|$)
RewriteRule ^ http://domain.com%{REQUEST_URI} [R,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Using htaccess to redirect to mobile version unless user wants to force desktop version
Try that:
RewriteEngine On
RewriteCond %{QUERY_STRING} !^desktop
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-mobile|iemobile|iphone|ipod|#opera mobile|palmos|webos" [NC]
RewriteRule ^$ http://www.example.com/mobile [L,R=302]
redirect to mobile site using htaccess
Well, just change
RewriteRule ^$ http://example.com/mobile [L,R=302]
to
RewriteRule ^/?(.*)$ http://example.com/mobile [L,R=302]
or maybe
RewriteRule ^/?(.*)$ http://example.com/mobile/$1 [L,R=302,QSA]
To redirect to a different hostname (sometimes called a "subdomain"), as asked in the comment below, you can do that:
RewriteRule ^/?(.*)$ http://m.example.com/$1 [L,R=302,QSA]
And a general remark: I think a http-301 does make more sense than a http-302 for this situation...
Mobile redirection using .htaccess - only on homepage
The rewrite rules below were tested in an .htaccess
file in the document root. Replace m.domain.com
and domain.com
as appropriate.
Additionally you can force mobile on or off for any URL by appending ?m=0
or ?m=1
, or setting the mredir
cookie with the scripting language (php in this case).
RewriteEngine On
# Put forced mobile state into 'mredir' cookie
RewriteCond %{QUERY_STRING} (?:^|&)m=([0-1])(?:&|$)
RewriteRule ^ - [CO=mredir:%1:.domain.com]
# Forced mobile? (m=1)
RewriteCond %{HTTP_HOST} !^m\.domain\.com$
RewriteCond %{QUERY_STRING} (^|&)m=1(&|$) [OR]
RewriteCond %{HTTP_COOKIE} ^.*mredir=1
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$)
RewriteRule ^ http://m.domain.com [R=302,L]
# Forced regular? (m=0)
RewriteCond %{HTTP_HOST} ^m\.domain\.com$
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$) [OR]
RewriteCond %{HTTP_COOKIE} ^.*mredir=0
RewriteCond %{QUERY_STRING} !(^|&)m=1(&|$)
RewriteRule ^ http://domain.com [R=302,L]
# Auto redirect to mobile if not forced above
RewriteCond %{HTTP_HOST} !^m\.domain\.com$
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$)
RewriteCond %{HTTP_COOKIE} !.*mredir=0
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]
RewriteCond %{HTTP:x-wap-profile}%{HTTP:Profile} !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-|dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-|maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv|palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany|sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo|teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi|wapp|wapr|webc|winw|winw|xda|xda-|up.browser|up.link|windowssce|iemobile|mini|mmp|symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC]
# Only auto-redirect on / or /index.php
RewriteRule ^(\/|index\.php)?$ http://m.domain.com [R=302,L]
.htaccess redirect to mobile and desktop in one file
...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"
Related Topics
Favicon Not Showing Up in Google Chrome Browser
Html,Css - How to Horizontally Center Two Tables Side by Side
Insert Line Break After Each Tr Tag Row
Input Type Date Appearance in Safari on Ios
Height: 100%; Not Working With Flex Box
Img Src Tag Not Displaying Image
React: Action on Variable Change
How to Prevent VS Code from Breaking Up Long HTML Lines into Multiple Lines
Make Div (Height) Occupy Parent Remaining Height
How to Remove Button Style or Overwrite Dynamically Added Classes to Button in Material2
How to Remove the Extra Space in in This Search Box (Between Input Field and Button)
Why Doesn't Height: 100% Work to Expand Divs to the Screen Height
Proper Use of Flex Properties When Nesting Flex Containers
How to Auto-Resize an Image to Fit a 'Div' Container
How to Make a Placeholder For a 'Select' Box
Html Table With 100% Width, With Vertical Scroll Inside Tbody
Html:How to Make a Curve Like This Using HTML & CSS Using Border or Box