This question already has an answer here:
- rewrite url with htaccess [closed] 1 answer
My problem:
I want to write a .htaccess rule for.
www.asdf.com/city.php?city=New-York
to www.asdf.com/New-York
but also with that I have other pages such as
www.asdf.com/country.php?country=USA
which I would like to appear as www.asdf.com/USA
and
www.asdf.com/state.php?country=LA
which I would like to appear as www.asdf.com/LA
Pretty confused how to do that.
If you wish to have identical url's for 3 completely different things, you are forced to use a php page to 'detect' what kind of 'thing' it is and 'route' your request to a specific page.
The following (untested) two rules will redirect all listed 'ugly' url's above to a fancy url and the fancy url to a php page that determines what page it should use for the request.
%2
is a back reference to the second capturing group in a RewriteCondition. The trailing?
clears the query string.With a file
myRouter.php
with something like:More logical would it be to use different fancy url's for different things, e.g.
/country/USA
forcountry.php?country=USA
,/state/LA
forstate.php?country=LA
and/city/New-York
forcity.php?city=New-York
.This can easily be done with the following (untested) htaccess and produces more logical url's: