Hacker
Hacker

Reputation: 7906

2 conditions in same .htaccess file.

I am trying to do this when ever i hit from a mobile i redirect to mobile website and from pc means normal website. But the conditions dont work properly.

<IfModule mod_rewrite.c>
  RewriteEngine on


  RewriteCond %{HTTP_USER_AGENT} "{android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile}" [NC]
  RewriteRule ^(.*)$ http://localhost:8080/home.html [L,R=301]

  RewriteCond %{HTTP_USER_AGENT} "!{android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile}" [NC]
  RewriteRule ^abc.html$ /abc1.html [R=301,L]
  RewriteRule ^1.html$ /2.html [R=301,L]

</IfModule>

when i hit localhost from a mobile device i get mobile website properly, but if i give localhost/abc.html the second condition comes into picture, but it actually should go to mobile website. Any problem with the code?

Upvotes: 0

Views: 191

Answers (1)

TerryE
TerryE

Reputation: 10878

Not so much an ans, but a set of points re your approach:

  • Why {} in your cond regexps instead of the correct () ?
  • The second cond is redundant since the inverse will alway match on the prevoius [L] rule? (Rule 3 doesn't have this cond BTW.
  • You should use a RewriteBase / as the engine sometime gets confused if you don't
  • Omit the leading / on the rules 2 and 3 replacement strings

Lastly and most importantly, why are you doing a 301 redirects instead of a simple internal redirect which then does not require the extra round trip and/or browser caching of 301s?

RewriteCond %{HTTP_USER_AGENT} (android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile) [NC]
RewriteRule ^(.*)$ mobile/home.html [L]

RewriteRule ^abc.html$ abc1.html    [L]

RewriteRule ^1.html$   2.html       [L]

Upvotes: 1

Related Questions