Currently I have this rewrite rule in place and it works great:
RewriteRule ^search/([^/]+)/? search.php?term=$1 [NC,QSA]
When I go to mysite.com/search/foo
it takes me to the proper search page for foo
. what I am looking to do is make the opposite work as well so that if I visit mysite.com/search.php?term=foo
I will be taken to mysite.com/search/foo
. I think I need a 301 redirect somewhere but I'm not sure how to do it without creating an infinite loop and the other posts I've read so far haven't been much help.
Try:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /search\.php\?term=([^&]+)
RewriteRule ^search\.php$ /search/%1/? [L,R=301]
or (if you place these after the rule that you already have
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} ^term=([^&]+)$
RewriteRule ^search\.php$ /search/%1/? [L,R=301]
Try this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+mysite.com/search.php?term=foo [NC]
RewriteRule ^ mysite.com/search/foo [L,R=301]
Add the R
flag to your RewriteRule
:
RewriteRule ^search/([^/]+)/? search.php?term=$1 [NC,QSA,R=301]