Redirect Single Page for Search Engine Query
Posted on September 10th, 2006by Michael Gray in Programming
If you're new here, you may want to subscribe to my RSS feed. Read my top posts or learn more about Michael Gray. Want more frequent updates follow me on Twitter. Thanks for visiting!
So if anyone reading this is an htaccess expert and can give a me a little help I’d appreciate it. I’m looking to trap for queries for a specific term from a search engine for a specific page and redirect them elsewhere. For example:
if the query was
http://www.google.com/search?q=foo*
and it went to
http://www.wolf-howl.com/foo/
I’d like to redirect to
http://example.com/foo/
for people who come to the page any other way I’d still like to show them
http://www.wolf-howl.com/foo/
Sphere: Related Content










September 10th, 2006 at 6:03 pm
RewriteCond %{HTTP_REFERER} google.com [NC]
RewriteCond %{HTTP_REFERER} q=foo [NC]
RewriteCond %{REQUEST_URI} ^/foo/$
RewriteRule ^/foo/$ http://example.org/foo/ [R,L]
If the referer contains “google.com”
and the referer contains “q=foo”
and the request was for exactly “/foo/”
then redirect the user to “http://example.org/foo/”.
Untested and of course has to be tuned to work with different strings for foo. RewriteCond backreferences can be a good friend in this case.
September 10th, 2006 at 6:24 pm
What would be the point of doing a redirect depending on where they are coming from though? Woudln’t you rather have everyone go to the same page no matter how they got there?
September 10th, 2006 at 6:33 pm
Johnathan, people coming from a SE are looking for a commercial result, others aren’t.
September 10th, 2006 at 6:55 pm
you should send all that chicken noodle soup traffic to a ringtones page
September 10th, 2006 at 8:00 pm
RewriteCond %{HTTP_REFERER} q=foo [NC]
to this
RewriteCond %{HTTP_REFERER} ^q=foo^ [NC]
to catch it no matter what order the parameters are in
September 11th, 2006 at 5:07 am
Would it make sense to check for q=foo before google.com, instead of putting every request from Google through to the second check?
September 11th, 2006 at 5:50 am
> RewriteCond %{HTTP_REFERER} ^q=foo^ [NC]
Hmm I don’t get this change. I am definitely no htaccess expert, so i’m intrigued to try to understand this.
Doesn’t this tell mod_rewrite only to match if “q=foo” is found between the beginning and the beginning of the HTTP_REFERER string?
September 11th, 2006 at 12:06 pm
What about all the other SE’s?
September 11th, 2006 at 12:24 pm
September 11th, 2006 at 1:19 pm
I was also wondering if “^q=foo^” works, or if you meant “*q=foo*”? Since ^ marks the start of the string and * would be a wildcard.
If you need to do any more fine tuning, the mod_rewrite cheat-sheet can come in handy for the basics:
http://www.ilovejackdaniels.com/mod_rewrite_cheat_sheet.png
September 17th, 2006 at 10:21 am
Michael?