Reputation: 8919
I've list of states and a php file which will list all the city of the state. By taking the URL parameter as /common.php/?state=Texas
and list all the cities.
After clicking on the city it takes the url as /common.php/?city=Alanreed&state_code=TX
and displays the city information.
I want to format the url using .htaccess like for states it should take state_name.html
eg. Texas.html
and for city name either Texas/sugar_land.html
or tx/sugar_land.html
How can I achieve this using php and .htaccess?
Upvotes: 0
Views: 194
Reputation: 13257
RewriteEngine On
RewriteRule ^(.*)/(.*).html$ common.php/?state=$1&city=$2 [L]
RewriteCond %{REQUEST_URI} !^(.*/)?index\.html$ [NC]
RewriteRule ^(.*).html$ common.php/?state=$1 [L]
\w{2}
means 2 word characters, so it is rewritten as a state code, the second rule is to rewrite the full state names and the third is to rewrite states.
Upvotes: 1
Reputation: 495
Try something like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([a-z]{2})\.[hH][tT][mM][lL]?$ common\.php\?state_code=$1
RewriteRule ^([a-zA-Z]+)\.[hH][tT][mM][lL]?$ common\.php\?state=$1
RewriteRule ^([a-z]{2})/([_a-zA-Z]+)\.[hH][tT][mM][lL]?$ common\.php\?state_code=$1&city=$2
RewriteRule ^([a-zA-Z]+)/([_a-zA-Z]+)\.[hH][tT][mM][lL]?$ common\.php\?state=$1&city=$2
</IfModule>
Should also work for tx.htm This assumes that your apache has mod_rewrite enabled.
Upvotes: 0