Mobile redirection using htaccess not working - apache

I have a website called http://www.mydomain.com.au
And a mobile sub domain www.m.mydomain.com.au
I use following htaccess codes
#Mobile Redirection
RewriteEngine on
RewriteCond %{HTTP_HOST} !^m\.mydomain\.com\.au$
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^/?(.*)$ http://m.mydomain.com.au/ [L,R=302]
Problem is when I type http://www.mydomain.com.au/anyurl from mobile it is not redirecting to mobile site. I just need to divert all pc website urls to my mobile website home page www.m.mydomain.com.au
Any help pls.

Think that if your mobile site url is www.m.mydomain.com.au
you should write :
#Mobile Redirection
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.m\.mydomain\.com\.au$
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^/?(.*)$ http://www.m.mydomain.com.au/ [L,R=302]
If it does not work, could you post your 2 vhosts ?
Regards;
mimiz

Related

.htaccess redirection resulting in infinite loop in my local

I'm trying to redirect the mobile traffic from one domain to another, and the desktop traffic too.
So when I go to www.a.com which is the desktop domain, with a mobile, .htaccess redirects to m.a.com. And when I go to m.a.com with a desktop, .htaccess redirects to www.a.com
This is working normal in a test server which uses desktop.domain.com and mobile.domain.com.
I tried it on my local with a domain like www.local.com and m.local.com but I get inifinite redirection loop when accessing the mobile version with a mobile UA, but not with a desktop UA.
THis is my desktop.a.com .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} !^desktop
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-mobile|iemobile|iphone|ipod|#opera mobile|palmos|webos" [NC]
RewriteCond %{HTTP_HOST} desktop\.local\.com [NC]
RewriteRule ^$ http://mobile.local.com [L,R=302]
This is my mobile.a.com .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mobile\. [NC]
RewriteCond %{HTTP_USER_AGENT} !(android|blackberry|googlebot-mobile|iemobile|iphone|ipod|opera\smobile|palmos|webos) [NC]
RewriteRule ^$ http://www.local.com [L,R=302]
As a side note, several images are missing in my local because I don't have those.

Redirect to mobile pages with different filename extension using .htaccess

I have created a desktop website at www.domain.com and a mobile site at m.domain.com., and am redirecting smartphone visitors to the latter using .htaccess:
# Mobile redirect #
RewriteEngine on
RewriteBase /
# prevent looping
RewriteCond %{HTTP_HOST} !^m.goldenhinde.com$
# 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|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]
RewriteCond %{HTTP_REFERER} !^http://(.*).goldenhinde.com/.*$ [NC]
# rewrite rules here
RewriteRule .? http://m.goldenhinde.com%{REQUEST_URI} [L,R=302]
However, the problem I have is that URLs for pages on my desktop site do not feature a filename extension (e.g. www.website.com/contact), whereas those on the mobile site have the .php suffix (e.g. m.website.com/contact.php).
When visitors enter an address such as www.website.com/contact on their smartphones, I would like them to be redirected to m.website.com/contact.php, but this is obviously not occurring at present due to the inclusion of the filename extension in the mobile version.
Can you advise what code I can add to my .htaccess file so the desktop pages automatically redirect to their mobile equivalent.
Thanks in advance for any help you can provide.
Add this rule to the end
RewriteCond %{HTTP_HOST} ^m.goldenhinde.com$ [NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [L,R=301]

Using htaccess to redirect to mobile version unless user wants to "force desktop version"

I have the following .htaccess file to redirect to the mobile version of my site:
RewriteEngine On
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]
However, I need to add a condition for mobile users to be able to request the desktop site.
I've searched around a bit and haven't found a simple answer. I just need to have an url such as http://www.example.com/?desktop to avoid the redirection to /mobile
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]

htaccess RewriteCond to let requests for website.com/m/... stay untouched

From a phone, if the user tries to access website.com, they are redirected to mobile.website.com. However, mobile.website.com makes AJAX requests to website.com, so I make all requests go through website.com/m/.... This isn't working:
# redirect phones/tablets to mobile site
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{HTTP_HOST} !mobile\.website\.com [NC]
RewriteCond %{REQUEST_URI} !^/m/ [NC]
RewriteRule ^(.*)$ http://www.mobile.website.com/$1 [L,R=302]
Specifically the line:
RewriteCond %{REQUEST_URI} !^/m/ [NC]
It should cancel the rewrite rule if the url matches website.com/m/.... Any ideas?
Thanks!
Change your code with this:
# redirect phones/tablets to mobile site
RewriteCond %{HTTP_USER_AGENT} (android|blackberry|ipad|iphone|ipod|iemobile|opera [NC]mobile|palmos|webos|googlebot-mobile) [NC]
RewriteCond %{HTTP_HOST} !mobile\.website\.com$ [NC]
RewriteRule ^(?!m/).*$ http://www.mobile.website.com%{REQUEST_URI} [L,R,NC]
Also if this still doesn't work then please post matching lines from your Apache's access.log and error.log.

htaccess to Redirect users to mobile site BUT still allow requests from the mobile site

I use the following snippet in my htaccess:
# redirect phones/tablets to mobile site
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{HTTP_HOST} !mobile\.website\.com [NC]
RewriteRule ^(.*)$ http://www.mobile.website.com/$1 [L,R=302]
However, inside my mobile app, I make AJAX requests to www.website.com/mobile, and they are all failing. How can I send these AJAX requests and still redirect them?
You can use:
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{HTTP_HOST} !mobile\.website\.com [NC]
RewriteCond %{REQUEST_URI} !^/mobile [NC]
RewriteRule ^(.*)$ http://www.mobile.website.com/$1 [L,R=302]
It will unblock the /mobile and your ajax requests will work.