Toni Michel Caubet
Toni Michel Caubet

Reputation: 20163

Redirect to mobile version, respecting domain, with htaccess

I have a site which has multiple domains, ie:

www.site.co.uk
www.site.ru
www.site-german.de

And the mobile version is in /mobile/

How can I redirect from the current domain to /mobile/ path?

I believe I need to check if the domain is one of the listed and the current path is not mobile, right? ( I am really bad with regular expressions.. )

This is what I have for now:

RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
    RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
    RewriteRule ^(.*)$ http://example.com/mobile/$1 [L,R=302]

Which works, but as you can see it doesn't check if the current path is '/mobile/' and it doesn't check for the other domains..

Any hints?

Upvotes: 0

Views: 297

Answers (1)

Koryonik
Koryonik

Reputation: 2768

Maybe by adding a RewriteCond to check the current path, with a negative regex group (?!...), as :

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "ipod|iphone|ipad|android|palm|blackberry|googlebot-mobile" [NC]
RewriteCond %{REQUEST_URI} ^(?!/mobile/)
RewriteRule ^(.*)  /mobile/$1   [QSA,L,R=301]

Upvotes: 2

Related Questions