John
John

Reputation: 163

mod_rewrite for query string

I have got 3 cases of mod_rewrite:

http://www.domain.com/location/albania/tirana/tirana-airport
http://www.domain.com/location/albania/tirana
http://www.domain.com/location/albania

The actual url for all these cases would be:

http://www.domain.com/page.php?country=albania&city=tirana&location=tirana-airport

Can you describe a mod_rewrite rule which can work for all 3 cases? Right now if a city or location is missing, the page returns 404. It only works if all the 3 parameters are present. Heres the rule I am using right now:

RewriteRule ^location/([^/]+)/([^/]+)/([^/]+)(.*)$ page.php?country=$1&city=$2&location=$3 

Thanks in advance!

Upvotes: 1

Views: 55

Answers (1)

anubhava
anubhava

Reputation: 785146

Replace your rule with this:

RewriteRule ^location/([^/]+)/?([^/]*)/?([^/]*)/?$ /page.php?country=$1&city=$2&location=$3 [L,QSA,NC]

It works with all 3 of your URLs.

Upvotes: 1

Related Questions