mod_rewrite to redirect URL with query string

Posted by meeble on Stack Overflow See other posts from Stack Overflow or by meeble
Published on 2012-12-12T01:25:53Z Indexed on 2012/12/13 23:03 UTC
Read the original article Hit count: 253

Filed under:
|
|

I've searched all over stackoverflow, but none of the answers seem to be working for this situation. I have a lot of working mod_rewrite rules already in my httpd.conf file. I just recently found that Google had indexed one of my non-rewritten URLs with a query string in it:

http://domain.com/?state=arizona

I would like to use mod_rewrite to do a 301 redirect to this URL:

http://domain.com/arizona

The issue is that later on in my rewrite rules, that 2nd URL is being rewritten to pass query variables on to WordPress. It ends up getting rewritten to:

http://domain.com/index.php?state=arizona

Which is the proper functionality. Everything I have tried so far has either not worked at all or put me in an endless rewrite loop. This is what I have right now, which is getting stuck in a loop:

RewriteCond %{QUERY_STRING} state=arizona [NC]
RewriteRule .*   http://domain.com/arizona [R=301,L]
#older rewrite rule that passes query string based on URL:
RewriteRule ^([A-Za-z-]+)$ index.php?state=$1 [L]

which gives me an endless rewrite loop and takes me to this URL: http://domain.com/arizona?state=arizona

I then tried this:

RewriteRule .*   http://domain.com/arizona? [R=301,L]

which got rid of the query string in the URL, but still creates a loop.

© Stack Overflow or respective owner

Related posts about query

Related posts about mod-rewrite