tags:

views:

16

answers:

1

I just don't get it:

Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /

RewriteRule ^([a-z]+\-[0-9]+)/?$ $1/home/ [R]
RewriteRule ^[a-z]+\-([0-9]+)/(home|alone)/?$ /$2.php?id=$1 [L]
RewriteRule ^.*$ http://www.anotherdomain.com/ [R=301]

why is the last rule (and by last I mean the redirection to anotherdomain.com) always processed?.

I need something like this:

http://mydomain.com/some-344 ---> http://mydomain.com/some-344/home/
http://mydomain.com/some-344/ ---> http://mydomain.com/some-344/home/
http://mydomain.com/some-344/home/ ---> home.php?id=344
http://mydomain.com/some-344/alone/ ---> alone.php?id=344
http://mydomain.com/anythingelse... --> http://www.anotherdomain.com/

thanks!

A: 

The last rule is always processed, because ^.*$ will always match. While you've specified the L flag on the second rule, it probably doesn't work quite like you expect.

It's also a good idea to make sure when redirecting to a local path, you include a leading slash, and when redirecting in general, you specify the L flag so the redirect happens immediately. Currently, it all works out OK, but if you look at the processing going on under the hood, it's doing things a little more messily than necessary.

As far as your actual issue goes, conditioning the catch-all redirect based on the original request to the server should get you what you wanted:

RewriteEngine on
RewriteBase /

RewriteRule ^([a-z]+\-[0-9]+)/?$ /$1/home/ [R,L]
RewriteRule ^[a-z]+\-([0-9]+)/(home|alone)/?$ /$2.php?id=$1

RewriteCond %{THE_REQUEST} !^[A-Z]+\s/[a-z]+\-[0-9]+/(home|alone)/?
RewriteRule ^.*$ http://www.anotherdomain.com/ [R=301,L]
Tim Stone
Wow! this works as I expected!!!, thanks for the tips.
coma