Michael Gray

Redirect Single Page for Search Engine Query

Posted on September 10th, 2006
by 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

Text Link Ads


11 Responses to “Redirect Single Page for Search Engine Query”

  1. User GravatarJan Says:

    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.

  2. User GravatarJonathan Says:

    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?

  3. User GravatarMichael Gray Says:
    That worked thanks Jan.

    Johnathan, people coming from a SE are looking for a commercial result, others aren’t.

  4. User Gravatargarrett Says:

    you should send all that chicken noodle soup traffic to a ringtones page ;)

  5. User GravatarMichael Gray Says:
    update you may need to change this line

    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

  6. User GravatarMike Says:

    Would it make sense to check for q=foo before google.com, instead of putting every request from Google through to the second check?

  7. User GravatarJan Says:

    > 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?

  8. User GravatarKen Savage Says:

    What about all the other SE’s?

  9. User GravatarMichael Gray Says:
    The page doesn’t rank in the other SE’s because they do things the right way for this query instead of taking advantage of silly users habits to show more adverts ;-)
  10. User GravatarMike Says:

    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

  11. User GravatarJan Says:

    Michael?