1

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

2
  • You have to escape +in redirect conditions, it's a special character. Use \+ or [+] instead. A +means one or more of the character directly before the symbol must occur. E.g. a+ matches "a", "aa", "aaa" ,... Commented Jan 16, 2016 at 11:58
  • @maxhb umm like this? RewriteEngine On RewriteCond %{QUERY_STRING} ^Animal[+]Business[+]Cards$ RewriteRule ^(.*)$ http://www.example.com/business/search-results.php?keywords=Animal[+]Business [R=301,L] Commented Jan 16, 2016 at 13:25

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Yay!!! this one works!!!! RewriteEngine On RewriteCond %{QUERY_STRING} ^keywords=([^+]+)\+([^+]+)\+([^&]+)&go=([^&]+)$ [NC] RewriteRule ^ %{REQUEST_URI}?keywords=%1+%2&go=%4 [NC,L,R] The other one did not! Thank You so much!
@User2018878 see the edited answer, Both solutions are working fine on my server now.
YES YES YES! they are!!!! wow thank you!! it is easily deleting "+cards" from any search query! this is exactly what I was looking for! Thanks a LOT!
can you tell me what part in this code is giving the command to remove "+cards" RewriteEngine On RewriteCond %{QUERY_STRING} ^keywords=([^+]+)\+([^+]+)\+([^&]+)$ [NC] RewriteRule ^ %{REQUEST_URI}?keywords=%1+%2 [NC,L,R]
@user2018878 The first regex capture group ([^\+]+) matches "Animal" the second \+([^\+]+) matches "+business" ,the third \+([^\&]+) matches +card and the fourth ([^&]+) matches "Search", you can see we are not using the third captured value %3 in the target url. This is how the "+card" gets removed from the target url.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.