redirect specific url for mobile devices - .htaccess

first post here ;-)
My question. How do I redirect mobile visitors, once they view a specific url within my website, to the mobile version of that specific url?
For example a mobile device user visits mydomain.com/thisurl then that url should be redirected only for mobile device users to mydomain/thisurl_mobile.
I have partly some htaccess code that might be a start? But not sure how to do this for a specific url? This is what I have so far (Btw it needs to be https):
<IfModule mod_rewrite.c>
# turn on rewrite engine
RewriteEngine on
# only detect smart phone devices if we are not on mobile site
# to prevent redirect looping
RewriteCond %{QUERY_STRING} !^desktop
# a bunch of smart phone devices
RewriteCond %{HTTP_USER_AGENT} "sony|symbian|nokia|samsung|mobile" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "windows ce|epoc|opera|mini|nitro" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "j2me|midp-|cldc-|netfront|mot" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "up\.browser|up\.link|audiovox" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "mini|nitro|j2me|midp-|cldc"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "netfront|mot|up\.browser|up\.link"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "audiovox|blackberry|ericsson,"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "panasonic|philips|sanyo|sharp|sie-"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "portalmmm|blazer|avantgo|dange"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "palm|series60|palmsource|pocketpc"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "smartphone|rover|ipaq|au-mic,"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "alcatel|ericy|vodafone\/|wap1\."[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "wap2\.|iPhone|android"[NC,OR]
# redirect google mobile bot
RewriteCond %{HTTP_USER_AGENT} "googlebot-mobile"[NC]
# if the request is from any one of the above devices
# redirect to mobile url
RewriteRule ^$ https://www.mydomain/thisurl_mobile [L,R=302]
</IfModule>

Related

Add '/mobile' to url on mobile device

I am trying to come up with an htaccess code that will allow me to redirect users on all mobile devices going to webpage that begins with www.example.com/blog or example.com/blog to specific page - pretty much the same link with the following extension: /mobile.
so that websites for mobile devices become:
www.example.com/blog -> www.example.com/blog/mobile
example.com/blog -> example.com/blog/mobile
How can I do that?
You can place this rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP:Profile} !^$
RewriteRule ^blog/?$ /$0/mobile [L,NC,R=302]
This is assuming you don't have a .htaccess inside blog/ directory.
If you already have a blog/.htaccess then use this rule in blog/.htaccess
RewriteEngine On
RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP:Profile} !^$
RewriteRule ^/?$ /blog/mobile [L,R=302]
If above header don't work then use user agent based checks:
RewriteCond %{HTTP_ACCEPT} "text\/vnd\.wap\.wml|application\/vnd\.wap\.xhtml\+xml" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "sony|symbian|nokia|samsung|mobile|windows ce|epoc|opera" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "mini|nitro|j2me|midp-|cldc-|netfront|mot|up\.browser|up\.link|audiovox"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "blackberry|ericsson,|panasonic|philips|sanyo|sharp|sie-"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "portalmmm|blazer|avantgo|danger|palm|series60|palmsource|pocketpc"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "smartphone|rover|ipaq|au-mic,|alcatel|ericy|vodafone\/|wap1\.|wap2\.|iPhone|android"[NC]
RewriteRule ^blog/?$ /$0/mobile [L,NC,R=302]

htaccess redirect to mobile along with redirect to https

I need to proceed 2 actions:
Permanently redirect from http to https (on mobile and on desktop versions)
If user uses mobile version - redirect to m.domain.com
The code I've tried to manage is following:
RewriteEngine On
RewriteBase /
# If the user is asking for the fullsite, set a cookie, remove the query, and stop.
RewriteCond %{QUERY_STRING} ^fullsite$
RewriteRule ^/?(.*)$ https://domain.com/$1? [L,CO=fullsite:yes:.domain.com]
# If the user is asking for the mobilesite, set a cookie, remove the query, redirect and stop.
RewriteCond %{QUERY_STRING} ^mobilesite$
RewriteRule ^/?(.*)$ https://m.domain.com/$1? [L,CO=fullsite:no:.domain.com]
# If the visit is from a mobile user agent, and there's no fullsite cookie, redirect and stop
# if the browser accepts these mime-types, it's definitely mobile, or pretending to be
RewriteCond %{HTTP_ACCEPT} "text\/vnd\.wap\.wml|application\/vnd\.wap\.xhtml\+xml" [NC,OR]
# a bunch of user agent tests
RewriteCond %{HTTP_USER_AGENT} "sony|symbian|nokia|samsung|mobile|windows ce|epoc" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "mini|nitro|j2me|midp-|cldc-|netfront|mot|up\.browser|up\.link|audiovox"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "ericsson,|panasonic|philips|sanyo|sharp|sie-"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "portalmmm|blazer|avantgo|danger|palm|series60|palmsource|pocketpc"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "smartphone|rover|ipaq|au-mic,|alcatel|ericy|vodafone\/|wap1\.|wap2\.|iPhone|android"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
# Note: I've updated the conditions above to skip iPads. However,
# mobile safari still reports itself as 'mobile' and thus needs an explicit opt-out
RewriteCond %{HTTP_USER_AGENT} !iPad [NC]
RewriteCond %{HTTP_COOKIE} !^.*fullsite=yes.*$ [NC]
RewriteRule ^/?(.*)$ https://m.domain.com/$1 [L,R=302]

How to Add 301 Redirect Code to htaccess

I need to add 301 redirect for few html pages on my website. My htaccess file already has a lot of code. How to add 301 redirect without messing up the htaccess file.
Here's what it looks like.
# turn on rewrite engine
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/blog/
# only detect smart phone devices if we are not on mobile site
# to prevent redirect looping
RewriteCond %{HTTP_HOST} !^m.easydestination.net$
# a bunch of smart phone devices
RewriteCond %{HTTP_USER_AGENT} "sony|symbian|nokia|samsung|mobile" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "windows ce|epoc|opera|mini|nitro" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "j2me|midp-|cldc-|netfront|mot" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "up\.browser|up\.link|audiovox" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "mini|nitro|j2me|midp-|cldc"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "netfront|mot|up\.browser|up\.link"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "audiovox|blackberry|ericsson,"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "panasonic|philips|sanyo|sharp|sie-"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "portalmmm|blazer|avantgo|dange"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "palm|series60|palmsource|pocketpc"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "smartphone|rover|ipaq|au-mic,"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "alcatel|ericy|vodafone\/|wap1\."[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "wap2\.|iPhone|android"[NC,OR]
# redirect google mobile bot
RewriteCond %{HTTP_USER_AGENT} "googlebot-mobile"[NC]
# if the request is from any one of the above devices
# redirect to mobile site'
RewriteCond %{REQUEST_URI} !\.(?:gif|jpe?g|png)$ [NC]
RewriteRule .? http://m.easydestination.net%{REQUEST_URI} [L,R=302]

Exclude Images from Mobile Redirect on htaccess

I am using htaccess to create a mobile redirect for my website. Basically it changes mysite.com to m.mysite.com. In the process it also changes the url of images. How can I prevent that? Below is the code I am using.
# turn on rewrite engine
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/blog/
# only detect smart phone devices if we are not on mobile site
# to prevent redirect looping
RewriteCond %{HTTP_HOST} !^m.easydestination.net$
# a bunch of smart phone devices
RewriteCond %{HTTP_USER_AGENT} "sony|symbian|nokia|samsung|mobile" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "windows ce|epoc|opera|mini|nitro" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "j2me|midp-|cldc-|netfront|mot" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "up\.browser|up\.link|audiovox" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "mini|nitro|j2me|midp-|cldc"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "netfront|mot|up\.browser|up\.link"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "audiovox|blackberry|ericsson,"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "panasonic|philips|sanyo|sharp|sie-"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "portalmmm|blazer|avantgo|dange"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "palm|series60|palmsource|pocketpc"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "smartphone|rover|ipaq|au-mic,"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "alcatel|ericy|vodafone\/|wap1\."[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "wap2\.|iPhone|android"[NC,OR]
# redirect google mobile bot
RewriteCond %{HTTP_USER_AGENT} "googlebot-mobile"[NC]
# if the request is from any one of the above devices
# redirect to mobile site
RewriteRule .? http://m.easydestination.net%{REQUEST_URI} [L,R=302]
You can add this, line before the last RewriteRule:
RewriteCond %{REQUEST_URI} !\.(?:gif|jpe?g|png)$ [NC]
To exclude all files with .gif .jpeg .jpg .png
Do not forget after to use absolute links for those images.

Link from mobile site after htaccess redirect to desktop version

I have a mobile version of a site http://www.m.fdl.de/
and a desktop version http://www.fdl.de/
With a .htaccess redirect I link all mobile users to the mobile version, what is running nice.
On my mobile Site I set a link in the footer that should link back to the desktop version.
I tried it with several .htaccess solutions I found on stackoverflow but with no success.
my link is always linking me back to the mobile site.
This is my .htaccess . I found it on this post: Mobile Redirect using htaccess
RewriteEngine on
RewriteBase /
# Check if this is the noredirect query string
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:www.fdl.de]
# 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} !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "wapp|wapr|webc|winw|winw|xda|xda-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "up.browser|up.link|windowssce|iemobile|mini|mmp" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC]
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]
# Check if we're not already on the mobile site
RewriteCond %{HTTP_HOST} !^m\.
# Can not read and write cookie in same request, must duplicate condition
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$)
# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP_COOKIE} !^.*mredir=0.*$ [NC]
# Now redirect to the mobile site
RewriteRule ^ http://m.fdl.de [R,L]
It woul be very nice if someon coul help me!
I might be really late, but i found out that the link to the full site must be like this:
Full Site
Hope it helped.

Resources