Redirect Single Page for Search Engine Query

Michael Gray

By Michael Gray
In Programming  

Print Post Print Post Email Post Email Post    ADD TO STUMBLEUPON Sphinn It ADD TO DEL.ICIO.US  Tweet This

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/

Related posts:

  1. How to Turn Off Personalized Search in Google Chrome With the impending arrival of Google OS I’ve been spending...
  2. How to Add Search Shortcuts to Google Chrome. Last week Danny Sullivan twittered he was looking for a...
  3. How TED Helped Me Understand Personalized Search Long time readers here will know that I’m not a...

Crazyegg Link Tracking

{ 11 comments }

Jan September 10, 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.

Jonathan September 10, 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?

Michael Gray September 10, 2006 at 6:33 pm

That worked thanks Jan.

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

garrett September 10, 2006 at 6:55 pm

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

Michael Gray September 10, 2006 at 8:00 pm

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

Mike September 11, 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?

Jan September 11, 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?

Ken Savage September 11, 2006 at 12:06 pm

What about all the other SE’s?

Michael Gray September 11, 2006 at 12:24 pm

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 ;-)

Mike September 11, 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

Jan September 17, 2006 at 10:21 am

Michael?

Comments on this entry are closed.