简体   繁体   中英

Appending query string through redirect htaccess

I am trying to create a rewrite cond in htaccess I am trying to redirect

http://www.example.com/business/search-results.php?keywords=Animal+Business+Cards&go=Search

to

http://www.example.com/business/search results.php?keywords=Animal+Business&go=Search

or if possible remove the word "+cards" from any search queries...

what I have tried so far

RewriteEngine On
RewriteCond   %{QUERY_STRING}   ^Animal+Business+Cards$
RewriteRule   ^(.*)$ http://www.example.com/business/search-results.php?keywords=Animal+Business  [R=301,L]

also tried

RewriteCond %{QUERY_STRING}    "&go=Search" [NC]
RewriteRule (.*)  /$1? [R=301,L]

and this

Redirect /business/search-results.php?keywords=Animal+Business+cards&go=Search http://www.example.com/business/search-results.php?keywords=Animal+Business&go=Search

None of these working... is it possible? Can any one help? Thank You in advance

Try the following code :

RewriteEngine On

RewriteCond %{QUERY_STRING} ^keywords=([^\+]+)\+([^\+]+)\+([^&]+)&go=([^&]+)$ [NC]
RewriteRule ^ %{REQUEST_URI}?keywords=%1+%2&go=%4 [NC,L,R]

Another solution

RewriteEngine On

RewriteCond %{THE_REQUEST} \?keywords=([^\+]+)\+([^\+]+)\+([^&]+)&go=([^&\s]+) [NC]
RewriteRule ^ %{REQUEST_URI}?keywords=%1+%2&go=%4 [NC,L,R]

Note : Redirect directive does not support querystrings in its old path perameter, that is why your last attempt failed. You can use %{QUERY_STRING} or %{THE_REQUEST} variable to match against urls with query strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM