Apache rewrite rules for creating user friendly urls

I am relatively new to rewrite and cant get the following to work. (clearly my domain is not testsite, but dont want my proper one going public as its not finished yet.)

i want to rewrite testsite/fishing/region/region.php?region=fife



then want to rewrite testsite/fishing/region/fishery/fishery.php?url=goldenloch



I am using the following rules

    RewriteRule ^fishing/([^/]*)\.php$ /fishing/region/region.php?region=$1 [L]
    RewriteRule ^fishing/([^/]*)/([^/]*)\.php$ /fishing/region/fishery/fishery.php?url=$2&region=$1 [L]

each rule works on its own but when combined only the last one work. I have added the [L] flag which i believe should stop any other rewrite rules of the condition is met. however this still doesnt work.


I tried this and it seemed to work:

RewriteRule ^/fishing/([^/]*)\.php$ /fishing/region/region.php?region=$1 [R,L]
RewriteRule ^/fishing/([^/]*)/([^/]*)\.php$ /fishing/region/fishery/fishery.php?url=$2&region=$1 [R,L]

The only difference from yours (I think) is the leading / and I used redirects (R) just to see it was working. You should be able to remove the R so the user doesn't see the real url in his browser.

Just in case you're not, I would recommend using a command line tool (like curl) and not a browser to test this, just so you avoid any caching or other annoyances:

curl -vv 'http://localhost/fishing/fife/goldenloch.php'

You'll see the Location header in the server's response, that's the redirect at work. In my case, I see:

Location: http://localhost/fishing/region/region.php?region=fife


Location: http://localhost/fishing/region/fishery/fishery.php?url=goldenloch&region=fife

Turns out i needed to do

    RewriteRule ^fishing/([^/]*)/([^/]*) fishery.php?url=$2&region=$1 [L]
    RewriteRule ^fishing/([^/]*) region.php?region=$1 [L]

with the longer query first and it worked fine.

Need Your Help

Pinax & Apache: deliver Attachments only to authenticated users

django apache pinax

I want that Apache and Pinax only deliver Attachments to authenticated users.

ServiceStack authentication with both [Authenticate] and [ValidateApiKey] attributes

authentication servicestack api-key

I have some endpoints decorated with the [Authenticate] attribute. Now a third party client has to access the same endpoint by using a shared API key.