So I have a webpage that queries some data based on this parameter search.php?state=AL
.
What I'm trying to do is write a rule in my .htaccess
file that will:
- translate
website.com/state/AL
intosearch.php?state=AL
- If a user specifically request
search.php?state=AL
then translate that into/state/AL
I accomplished step 1 using this:
RewriteRule ^state/([A-Za-z][A-Za-z]) /search.php?state=$1 [NC]
How can I accomplish step 2? I know I will have to use [R, NC]
to actually rewrite the URL, but thats where I'm stuck.
EDIT: Not sure if this matters but my webhost forces me form some reason to add RewriteB开发者_如何转开发ase /
to the top of my .htaccess
file.
You want to perform the redirection only in the case that the original request was pointed to the /search.php?state=
URL, like so:
RewriteEngine On
# Externally redirect /search.php?state=XX --> /state/XX
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/search\.php\?state=([A-Z]{2}) [NC]
RewriteRule ^ http://%{HTTP_HOST}/state/%1? [R=301,L]
# Internally redirect /state/XX --> /search.php?state=XX
RewriteRule ^state/([A-Z]{2}) /search.php?state=$1 [NC]
Something like this would do the trick:
RewriteRule ^state/([A-Za-z][A-Za-z]) /search.php?state=$1 [NC, L]
RewriteCond %{QUERY_STRING} state=([A-Za-z][A-Za-z])
RewriteRule ^search\.php$ /state/%1 [R=303, NC]
精彩评论