MGGM82,
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 (http://dk.co.nz/seo#example-14 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
/business/search-results.php?keywords=Animal+Business+Cards&go=Search
to
http://www.mydomain.com/business/search-results.php?keywords=Animal+Business&go=Search
using
RewriteCond %{QUERY_STRING} ^keywords=([^+]+)+([^+]+)+([^&]+)$ [NC]
RewriteRule ^ %{REQUEST_URI}?keywords=%1+%2 [NC,L,R]
but was failing (for the following reasons):
-
+ 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.
-
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.
-
While not a cause of the failure, the No Case flag is both superfluous and inappropriate.
-
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).
-
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.
-
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.
^(keywords=.+)[+]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:
business/search-results.php?%1+photos+and+pictures%2
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.
Regards,
DK