Forum Moderators: phranque

Message Too Old, No Replies

.htaccess redirect

trying to point folks in the right direction...

         

bobothecat

10:43 pm on Mar 25, 2007 (gmt 0)



I need to redirect folks coming in when a search query includes *keyword*.

Example:

I do a search on big search engine... the search term includes the word *widget*, but it's going to the wrong page ( don't ask me how, as the keyword *widget* isn't even mentioned/linked on that page )... how can I redirect it to the correct page it's supposed to go to if *widget* is included in the search/referer?

Jim? :)

jdMorgan

11:36 pm on Mar 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's fairly easy if you only intend to "support" only a few of the search engines with this feature. But unfortunately, most of the engines use their own query string parameters, and few are the same. We have q=, p=, kw=, etc. all being used to denote where in the referrer string the search keywords are located.

But generically, a simple solution for G, Y, and MSN/Live might be something like:


RewriteCond %{HTTP_REFERER} search[^?]\?([^&]+&)*(as_q¦as_epq¦q¦p¦va¦what)=([^&+%]+(\+¦\%2b¦\%20))*big(\+¦\%2b¦\%20)blue(\+¦\%2b¦\%20)widget(\+¦\%2b¦\%20¦&)?
RewriteRule ^wrong_page\.html http://www.example.com/right_page.html [R=302,L]

All the extra stuff around "big blue widget" is intended to prevent keywords like "unbig blue widgetations" from matching -- that is, to be sure that the word(s) are bounded by "+" or spaces or by "q=" or "&", and not by more characters. Also, spaces between the words will be replaced with "+" or they will be encoded, or both, as indicated by the patterns.

Likewise, I'm enforcing the "boundaries" of the "q=" names to avoid ambiguity.

I use a 302 redirect, because you certainly don't want the page replaced in the search listings, and also to avoid cache-related problems, since the page-replacement function is dynamic and temporary.

The code is untested; You'll likely need to play with it to get it working exactly the way you want.

Jim

[edited by: jdMorgan at 3:26 pm (utc) on Mar. 29, 2007]