Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite referrer based redirection

         

Dman91

8:03 pm on Nov 26, 2010 (gmt 0)

10+ Year Member



My .htaccess looks like this:

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^listings/jobsall/(.+[^/])/(.+[^/])$ /resources/newjobs/listings/jobsall/$1/$2/ [R]
RewriteRule ^listings/jobsall/(.+[^/])/(.+[^/])/$ index.php?jobtype=1,2&orderby=$1&sortby=$2
RewriteRule ^listings/jobsall/(.+[^/])$ /resources/newjobs/listings/jobsall/$1/ [R]
RewriteRule ^listings/jobsall/(.+[^/])/$ index.php?jobtype=1,2&orderby=$1
RewriteRule ^listings/jobsall$ /resources/newjobs/listings/jobsall/ [R]
RewriteRule ^listings/jobsall/$ index.php?jobtype=1,2
RewriteRule ^listings/jobs/(.+[^/])/(.+[^/])$ /resources/newjobs/listings/jobs/$1/$2/ [R]
RewriteRule ^listings/jobs/(.+[^/])/(.+[^/])/$ index.php?jobtype=1&orderby=$1&sortby=$2
RewriteRule ^listings/jobs/(.+[^/])$ /resources/newjobs/listings/jobs/$1/ [R]
RewriteRule ^listings/jobs/(.+[^/])/$ index.php?jobtype=1&orderby=$1
RewriteRule ^listings/jobs$ /resources/newjobs/listings/jobs/ [R]
RewriteRule ^listings/jobs/$ index.php?jobtype=1


I would like to check for a referrer in the rules with jobsall and if it matches only then redirect.

How would I do that?

Also is there any better way of handling trailing /'s?

g1smd

8:46 pm on Nov 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need the [L] flag on the end of every rule.

The [R] flag gives a 302 redirect. A 301 redirect is usually recommended.

For redirects, the target should also include the protocol and domain name.

Additionally, the redirects should all be listed before any of the rewrites.

Referrer is a very unreliable test. Security software and many ISPs strip this data from user requests. Cookies are usually a much better bet.

Dman91

2:29 am on Nov 27, 2010 (gmt 0)

10+ Year Member



Hello g1smd,

Thanks for the reply - but I really need to do the referrer test.

Do you have any idea how to do it?

wilderness

3:13 am on Nov 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



n addition to what glmsd has provided. . . .

Patterns such as the following are definitely not my cup of tea:

(.+[^/])/(.+[^/])$

You should be able to modify such references to the following, which is merely provided as a method example:

# The refer contains a specific phrase and the requested page is one of the URI's (multiple conditions)
RewriteCond %{HTTP_REFERER} any-phrase-contained-in-refer
RewriteCond %{REQUEST_URI} one-page.html [OR]
RewriteCond %{REQUEST_URI} /directory1/page-two.html[OR]
RewriteCond %{REQUEST_URI} /directory2/page-three.html
RewriteRule .* http://example.com/directory/Newpage.html [L]

Of course you'll need to separate "jobsall" references from your other lines, and in addition (depending upon the numbers of references and/or different output pages desired), may require multiple sections, similar to the example I've provided.

Please keep in mind, as glmsd provided, the refer value is less than 100% effective.

g1smd

7:23 pm on Nov 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I missed that .+[^/] pattern.

A much better replacement would be ([^/]+) here.

jdMorgan

10:45 pm on Dec 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not necessarily, because the purpose may be to detect one or more of *any* characters followed by any single character which is not a slash -- It is a most likely a slashless URL detector. The only problem I see is that the 'trailing' part of each matched path must be at least two characters long with this pattern, so contrary to usual advice, ".*[^/]$" might be better if one-character trailing paths might need to be supported.

HTTP Referrers are optional for the sending client and intervening caching proxies, easily spoofed, and therefore unreliable.

However, if you wish to continue with the plan to use the referrer to condition your rules, you can either add a RewriteCond to each rule to check %{HTTP_REFERER}, or you can write a "skip rule" using the [S=nn] flag, so that "nn" following rules will be skipped if the referrer is (or is not) the one you specify.

Be especially careful to handle the case where legitimate visitors do not provide any referrer. They will almost certainly be unaware of this, and you must not treat them as "troublemakers." Users of AOL, Earthlink, other big ISPs, and many corporate networks may arrive at your site without any referrer.

Jim