Forum Moderators: phranque

Message Too Old, No Replies

Redirect based on .htaccess Referrer (SE search term)

Direct users to landing domain for search term

         

eyedmax

3:09 pm on Mar 29, 2007 (gmt 0)

10+ Year Member


Situation:
Site about "foo" (http://foo.com/) is also in search results for "bar" and "bar2".

I want to redirect all SE users with "bar" and "bar2" search terms to http://bar.com/bar_landing.html

And I want to analyze not only one SE - i.e. referrer's formats may differ.
For ex.:
^http://(www)?\\.?google.*, q= for Google
^http://search\\.yahoo.*, p= for Yahoo
^http://search\\.aol.*, userQuery= for AOL
^http://search\\.lycos.*, query= for Lycos
etc.

Referrer must be untouched, all terms except "bar" and "bar2" must proceed as usual.

Any ideas?

jdMorgan

3:28 pm on Mar 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



eyedmax,

Welcome to WebmasterWorld!

See this recent thread [webmasterworld.com] for a start, then post any questions back here.

Jim

eyedmax

4:57 pm on Mar 29, 2007 (gmt 0)

10+ Year Member



Thanks for your answer, Jim.

Hmmm...
It's close, but needs a lot ow work on it...
And I want to get a more "readable" solution...
Smth like this (don't bite me for syntax, it's nuts)

======================
$key1 = foo
$key2 = foo2

ConditionWithPattern [googleblabla...]
ConditionWithPattern [googleblabla...]
ConditionWithPattern [AOLblabla...]
ConditionWithPattern [AOLblabla...]

RedirectIfHit * [foo.com...]
=======================

Simple to modify, simple to add/edit SEs, human readable.

eyedmax

6:42 pm on Mar 29, 2007 (gmt 0)

10+ Year Member



RewriteCond %{HTTP_REFERER} google\.[^?]+[\?&].*q=([^&+%]+(\+¦\%2b¦\%20))*sample(\+¦\%2b¦\%20)bar(\+¦\%2b¦\%20¦&)?
RewriteCond %{HTTP_REFERER} search\.yahoo\.[^?]+[\?&].*p=([^&+%]+(\+¦\%2b¦\%20))*sample(\+¦\%2b¦\%20)bar(\+¦\%2b¦\%20¦&)?
RewriteRule ^ [bar.com...] [R=302,L]

Should it work?
This is what I call dumb construcrion :(

Is any type of "variable" in htaccess?

[edited by: jdMorgan at 12:53 am (utc) on Mar. 30, 2007]
[edit reason] Disabled smilies in code [/edit]

jdMorgan

1:05 am on Mar 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For both cases (google and yahoo), but using the google line as an example, I'd change

google\.[^?]+[\?&].*q=

to

google\.[^?]+\?([^&]+&)*q=

to make the processing of the characters preceding "p=" less ambiguous and therefore faster. Note that you don't need to escape the "?" when it's part of a grouped set of alternatives.

Other than that, it looks OK -- Test it with a user-agent spoofer (I use the "PrefBar" Firefox extension, which has this facility, as does the "Web Developer" extension), and see if it works.

Derivation, construction, and experimentation are how most folks learn this stuff -- The documentation leaves quite a bit of room for interpretation, and of course, there are an infinite number of regular expressions -- both valid and invalid... :)

[added] You *will* need to add an [OR] flag to all but the final RewriteCond, since you want the rule to execute if any of the referrer-bot RewriteConds are true. [/added]

Jim

[edited by: jdMorgan at 1:07 am (utc) on Mar. 30, 2007]

eyedmax

12:33 pm on Mar 30, 2007 (gmt 0)

10+ Year Member



Thanks for your advices.

But two things are still not clear for me.

1. Is any type of "variable" in htaccess?
2. If they, how to make code more user friendly (i.e. simple to change things)

added
3. I can enable/disable ref sending in PrefBar... How do you change referrer itself?

jdMorgan

2:53 pm on Mar 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Mod_rewrite can use system variables (e.g. %QUERY_STRING}, etc.), user-defined variables (See the [E=name:value] flag of RewriteRule), and back-references (see mod_rewrite documentation) to the matching value of parenthesized subpatterns. However it cannot compare two variables except by using an associativity trick, and this is only available if the server's operating system has a POSIX 1003.2 regular-expressions library which supports "atomic back-references." This makes the technique too server-dependent to be considered "portable."

To clarify the "associativity" aspect, using POSIX 1003.2 atomic back-references, you can do an indirect compare of two variables using the fact that if A+B is equal to A+A, then A and B are equal. But again, this technique is too OS-dependent to be relied upon.

mod_rewrite code and regular expressions are certainly not user-friendly. On the other hand, they can be used to create extremely compact and powerful functions, and are best seen in that light. If you want user-friendliness, then look into using a RewriteMap to invoke a PERL script that accesses a database to retrieve URL translations. If you do that, then the "friendliness" can be embodied in the database GUI, albeit at the cost of a lot of up-front work. You'll also need to have access to the server configuration files to define the RewriteMap; This is not available in .htaccess, although once defined, a map can be used in .htaccess.

Jim