Redirect search query string with edited search query

I want to create a rewrite cond in htaccess
I want to redirect


or if possible remove the word “+cards” from any search queries… is it possible?
how please
Thank You in advance


Of course it is!

IF you have mod_rewrite enabled, you can use a RewriteCond to test for +Cards in the query string and redirect to the {REQUEST_URI} with a modified query string (without +Cards).

Give it a try then show your code for more help.

In general, if you need information about mod_rewrite, I’ve got a long tutorial with sample code at



1 Like

yes thank you for your response
got it working with this code

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


What you have will remove the third and successive words from your keywords string (to the end of the keywords value) as well as any successive key/value pair (assuming keywords is the first key in your query string)… If you’re just looking to remove +cards (or +Cards), use (+[c|C]ards) to specify what you’re looking to remove.

IMHO, you’ve defined your question in very specific terms but coded very loosely. I’ve taught “Specificity” for years as it’s far easier to code to a good specification.

Finally, IMHO there is NEVER a valid use for the No Case flag in a RewriteRule (because the {REQUEST_URI} is case sensitive). Please learn what the code and flags mean before using them indiscriminately.



I have not coded it, someone helped me with it so can you tell me what will be the exact code if I am lookig to remove just (cards) and yes “keywords” is the first key in query string :smile:if you can tell me the exact code I can copy and paste I iwll be highly highly obliged: )
Thank You so much for your response!

Ah, ha! The plot thickens!

Specificity: keywords in any position and simply remove cards:

RewriteEngine on
# In your DocumentRoot
RewriteCond %{QUERY_STRING} ^(.+&)?keywords=([a-zA-Z0-9\+])+(\+[c|C]ards)([+&].+)?$
RewriteRule ^business/search-results\.php$ business/search-results.php?%1keywords=%2%4 [R=301,L]

That would be my RewriteCond if the key/value pair were not necessarily the first pair in the query string (nor the last). The code could be simplified by replacing the %2 code with (\w+)* for Card (or cards) at any place in the value or (\w+)+ if NOT the first in the query string. Note that the METACHARACTER + is being escaped to signify the PLUS character. The [c|C] handles the capitalization of the first letter in cards and ([+&}.+)? handles the case where there is another word after cards in the value or another key/value pair after cards and is optional if there is nothing after cards.

As for the flags, R=301 will display the redirection with the new query string and can be used for testing (to ensure it is redirecting correctly) then omitted if you do not want to show the removal. The Last flag signifies that Apache should execute the redirection immediately. The lack of the No Case flag prevents BuSiNeSs/SeArCh-ReSuLtS.pHp being accepted as the {REQUEST_URI} (it does NOT impact the query string at all because the RewriteRule cannot examine the query string).

If you need more information about mod_rewrite coding (including flags) with quite a few useful examples, has helped SitePoint members for over 10 years.



1 Like

oh wow! this is super and the way you have explained it in so much detail!
I am saving all solutions in a text file for later reference so this detailed explanation works wonders!
thank you so much!
thanks for the link too!
much appreciated!


Your question was a good one and led to some interesting options in the “specification” and coding so I have taken the liberty to summarize below and have included that in my mod_rewrite tutorial ( will be posted soon when I get the PDF and text versions made so I can upload the modified page).

Remove – or Replace – a Word in a Query String

I came across a new problem where someone wanted to redirect




RewriteCond %{QUERY_STRING} ^keywords=([^+]+)+([^+]+)+([^&]+)$ [NC] RewriteRule ^ %{REQUEST_URI}?keywords=%1+%2 [NC,L,R]
but was failing (for the following reasons):

  1. + is a metacharacter in regular expressions. To match a + character, it must be either escaped ( \+ ) or wrapped in a character range definition ( [+] ). Clearly, the +'s between groups were supposed to be the + character but were not escaped and the RewriteCond cannot match.

  2. The third group (which was supposed to match +Cards) is one or more characters until an ampersand ( & ) is found … or the end of the string. Because the URI’s query string ends with &go=Search, the RewriteCond cannot match.

  3. While not a cause of the failure, the No Case flag is both superfluous and inappropriate.

  4. The RewriteRule’s regex, simply the start anchor ( ^ ), is not incorrect but will match EVERY {REQUEST_URI} and may cause problems should the RewriteCond ever match (I prefer .? to force a match but the effect would be the same).

  5. The RewriteRule’s redirection correctly uses the %{REQUEST_URI} and recreates the query string to be* keywords={first group}{+second group}* which is fine except that &go=Search has also been removed.

  6. The No Case flag is never appropriate in a RewriteRule (because %{REQUEST_URI} is case sensitive). The Last flag is fine (it causes Apache to execute the redirection) and the Redirection Status (R or R=301) flag tells Apache to display the redirection in the browser. This was not specified but is what I would have done with it.

Back to the original intention (remove Cards from the value of keywords), we will need to find* keywords=* in the query string and Cards in the value. To restore the query string without Cards, we will need to capture everything before Cards and everything after Cards.


This regex specifically captures two groups, everything before +Cards (beginning with keywords=) and everything after +Cards (the &go=Search). Please note that my RewriteCond regex is capturing all characters including the* +'s and &'s with either (.+)*.

I prefer to specify the %{REQUEST_URI} for which this RewriteRule (set) will operate, too, so my first attempt would be (in the DocumentRoot’s .htaccess):

RewriteCond %{QUERY_STRING} ^(keywords=.+)[+]Cards(.+)$ RewriteRule ^business/search-results\.php$ business/search-results.php?%1%2 [R=301,L]
Remove the R=301, from the flags if the redirection is not to be displayed.

The person asking the question probably made a typo changing Cards to cards at one point so let me handle the capitalization … and the plural at the same time. Examining only the %2 created by the RewriteCond, [+]Cards, the capitalization can be handled by changing the C to* [c|C]* and the plural handled by making the* s* optional with the result being* [+][c|C]ards?*.

RewriteCond %{QUERY_STRING} ^(keywords=.+)[+][c|C]ards?(.+)$ RewriteRule ^business/search-results\.php$ business/search-results.php?%1%2 [R=301,L]
For security purposes, I would change the %2 to only match &go=Search so my recommended code for the original question (with lack of clarity on “Cards”) would be:

RewriteCond %{QUERY_STRING} ^(keywords=.+)[+][c|C]ards?(&go=Search)$ RewriteRule ^business/search-results\.php$ business/search-results.php?%1%2 [R=301,L]
To replace Cards with any other string, you will need to remember to insert encoded spaces (+'s) before each word in your string. Thus, to insert “photos and pictures”, the redirection would become:


The only thing remaining to make this a truly generic code (but retaining the keywords key/value pairing) would be to allow other (unspecified) key/value pairs before keywords (and retain the generic “catch-all” after Cards).

RewriteCond %{QUERY_STRING} ^((.+&)?keywords=.+)([+]Cards)(.+)$ RewriteRule ^business/search-results\.php$ business/search-results.php?%1%3 [R=301,L]
Note that I have made the query string before keywords optional so I needed to increment the trailing query string group to %3 in the redirection. If there is nothing before keywords, %2 is null and will not affect the redirection.

IF I have forgotten anything, please let me know and I’ll update the tutorial page again.



how wonderful! I am glad it will help others in future :slight_smile: Thank You for doing this! :slight_smile:


That had been my aim while serving on SitePoint staff and remains because I believe sharing knowledge is important. From my perspective, I thank YOU for adding to my “I’ve seen everything” helping members with their mod_rewrite (and other) problems.



1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.