I am trying to redirect a URL with a query string to a URL without a query string, however, I would like to use values from the query string.
In my example below I am using a single state however I want it to be dynamic, meaning it will parse any URL with a query string down to a clean URL which still contains the value from the query string.
Example: http://www.mywebsite.com/?state=CA redirects to http://www.mywebsite.com/CA/
I have tired to add the information below into my HTACCESS file with no luck:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?state=([a-z]+)&type=([a-z\-]+)
RewriteRule ^ /%1/%2? [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?state=([a-z]+)($|\ )
RewriteRule ^ /%1? [L]
These have worked however I must input every redirect which I would like to avoid:
RewriteCond %{QUERY_STRING} state=FL
RewriteRule ^$ "http://www.mywebsite.com/fl/?" [R=301,L]
RewriteCond %{QUERY_STRING} state=RI
RewriteRule ^$ "http://www.mywebsite.com/ri/?" [R=301,L]
Any ideas? I am using WordPress on Apache. I believe everything is up to date.
The reason why the original rules won’t work is because it seems like your state is:
The
FLwill never match the regular expression[a-z]+, because[a-z]means literally any lowercase letter, and neither F nor L are lowercase. Try:So the
[NC]flag in the condition makes it so the regular expression is case insensitive.