Wednesday, January 19, 2011

apache mod_rewrite stop after first condition

Hi!

I'm trying to have this URL rewritten as follow:

  • fr.domain.com/tag/movies » domain.com/catch.php?country=fr&tag=movies
  • domain.com/tag/movies » domain.com/catch.php?tag=movies
  • fr.domain.com » domain.com/catch.php?country=fr

for these, I have the following mod_rewrite rules on a .htaccess file

  • subdomain + tag

    RewriteCond %{HTTP_HOST} ^([^.]+).domain.com [NC]

    RewriteRule ^tag/([^/]+) catch.php?country=%1&tag=$1 [L,NS]

  • tag

    RewriteRule ^tag/([^/]+) catch.php?tag=$1 [L,NS]

  • subdomain

    RewriteCond %{HTTP_HOST} ^([^.]+).domain.com [NC]

    RewriteRule ^(.*)$ catch.php?country=%1 [L,NS]

in this order exactly.

Now, the L after the first RewriteRule should end processing and redirect it to the subdomain+tag URL, but it doesn't. For example, fr.domain.com/tag/movies is sent to catch.php?country=fr, which is the last condition in the .htaccess file.

Shouldn't it stop after the first L found?

thanks in advance, cheers

ANSWER

well, it turns out that the [L] flag works differently depending on the context (.htaccess/<directory> different from httpd.conf) as you can see here.

So, to make it work just place the rules inside the <VirtualHost> (NOT <Directory>) and add a '/' to every URI (/catch.php, /tag, ...). That's it.

  • The L flag won't stop processing under all circumstances. I'd suggest checking out this thread on Stack Overflow, especially the link to the Apache Rewrite Guide, and also turning on the RewriteLog so that you can confirm how mod_rewrite is processing your rules.

    From jlupolt

0 comments:

Post a Comment