Forum Moderators: phranque

Message Too Old, No Replies

Redirect based on words in search engine query?

we don't offer what they're looking for

         

Edouard_H

11:08 am on May 27, 2005 (gmt 0)

10+ Year Member



I'm trying to redirect visitors who are searching for things my site doesn't offer to a specific page; (The underlying situation would be a topic for a whole 'nuther thread). I don't want to block or redirect visitors from the search engine in question, just those queries containing certain words.

This is what I have so far (RewriteEngine On) and it has no effect. I figured I would start out with as specific a URL as I could then try to pare it down to wildcards(?) once it's working. Unfortunately that hasn't happened.

RewriteCond %{HTTP_REFERER} ^http://searchdomain\.com/results\.aspx?FORM=BLAH&srch_type=0&q=[-.]word[-.]$
RewriteRule /* [mydomain.com...] [R,L]

Side question: Is there a broader method that would work for all SE domains? As of now this is only a problem with one...

jdMorgan

4:58 pm on May 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A slightly-modified pattern of

RewriteCond %{HTTP_REFERER} ^http://(www\.)?searchdomain\.com/results\.aspx.*[?&]q=([^&]+)

will result in the search term or phrase being copied into variable %2 for later use, regardless of where the search term(s) appear in the query string. This code also makes a leading "www." optional, so it can accept queries from the search engine domain with or without "www."

You would follow this with a final RewriteCond and RewriteRule something like:


RewriteCond %2 ^(word1¦word2¦word3) [NC]
RewriteRule .* http://www.example.com/page.html [R=302,L]

To add more search engines, you'd just insert tham after the first referrer-based RewriteCond above, adding an [OR] flag to that first one.

There are too many formats used by search engines to solve the problem with just one RewriteCond; Some use "p=", some use "qkw=", some use "query", etc. Therefore, I'd recommed handling this problem on an "as-needed" basis.

Replace the broken pipe "¦" characters with solid pipe characters (usually Shift-\) before use. Posting on this forum modifies them.

Jim

Edouard_H

6:37 pm on May 27, 2005 (gmt 0)

10+ Year Member



Thanks Jim for your generous guidance. Is the %2 variable an example of RewriteCond backreferences and if so is the significance of the "2" that it references the second set of parentheses in the previous RewriteCond?

Haven't quite got it working yet; Does the "+" in q=([^&]+) assume the presence of "+" in the query string?

jdMorgan

9:35 pm on May 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[^&]+ means "match one or more characters not equal to &" -- In this case, match the searched-for terms up to the following & -- if any.

Please see the references cited in our forum charter.

Jim

Edouard_H

10:22 pm on May 27, 2005 (gmt 0)

10+ Year Member



Please see the references cited in our forum charter.

Pretty much what I've been doing, albeit at a sloth's pace!

What I currently have (caret removed from second RewriteCond):

RewriteCond %{HTTP_REFERER} ^http://(www\.)?search\.domain\.com/results\.aspx.*[?&]q=([^&]+)
RewriteCond %2 (word¦word) [NC]
RewriteRule .* http://www.example.com/page.html [R=302,L]
Apparently I've created a loop because the search result page hangs when the listing is clicked and my server log shows repetitive 302s to the redirect page. Error log shows nothing. Checking to see if there are any obvious conflicts with other rules.

Thanks.

[edited by: jdMorgan at 11:25 pm (utc) on May 27, 2005]
[edit reason] Example.com [/edit]

Edouard_H

11:08 pm on May 27, 2005 (gmt 0)

10+ Year Member



Just as follow-up, redirected to a page on a different domain until I get the loop fixed and it works like a charm. Thanks!

jdMorgan

11:25 pm on May 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Loop-stopper:

RewriteCond %{HTTP_REFERER} ^http://(www\.)?search\.domain\.com/results\.aspx.*[?&]q=([^&]+)
RewriteCond %2 (word¦word) [NC]
RewriteRule [b]!^page\.html$[/b] http://www.example.com/page.html [R=302,L]

[added] That's for .htaccess. If using this in httpd.conf, add a leading slash to the page.html pattern. [/added]

Jim