Itai Sagi
Itai Sagi

Reputation: 5615

mod_rewrite by user agent not working?

I want to achieve the following, if someone enters site with Android/iPhone user agent, I want to rewrite all his urls to a directory, so for example:

user enters -> user served
example.com -> example.com/mobile
example.com/review/12 -> example.com/mobile/review/12

I have the following example code snippet in my .htaccess, but it's not working...

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} Android
RewriteRule ^(.*)/$ mobile/$1

Upvotes: 2

Views: 2302

Answers (1)

Ulrich Palha
Ulrich Palha

Reputation: 9509

Try adding the following to your htaccess file in the root directory of your site.

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_USER_AGENT} Android [NC]
RewriteCond %{REQUEST_URI} !^/mobile [NC]
RewriteRule ^(.*)/?$ mobile/$1 [L]

If you want to change what the user sees in their browser address bar, change the last line to

RewriteRule ^(.*)/?$ mobile/$1 [L,R]

Upvotes: 3

Related Questions