Forum Moderators: phranque

Message Too Old, No Replies

Rewrite to stop all access

to one page when without referrer

         

wm2007

11:16 pm on Oct 7, 2006 (gmt 0)

10+ Year Member



Hi

I need to rewrite all visitors to one page, when visitors that want access to this page have no referrer.

I also need a rewrite for a directory, when visitors that want access to this directory have no referrer.

webpage.htm and /directory/ redirected to /.

Anyone care to help me out with this?

:)

encyclo

1:00 am on Oct 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just check
{HTTP_REFERER}
to see if it's blank, and do the redirect if this is the case. See
RewriteCond
in the mod_rewrite documentation [httpd.apache.org], and there are some useful generic rewrite examples in the Apache forum library [webmasterworld.com] that you can adapt quite easily. :)

Don't forget that many users and all search engine bots don't send a referrer, and as such could never access the pages in question.

wm2007

9:11 am on Oct 8, 2006 (gmt 0)

10+ Year Member



Thank you encyclo

The page and directory should not be listed in search enginees, so this is just to stop them from crawled by such (also have them in my robots.txt, but that dont stop them).

I am all new to rewriting, will this stop all visitors without a referrer to webpage.htm and /directory/:


RewriteCond %{HTTP_REFERER} ^-?$
RewriteCond %{REQUEST_URI}!^/directory/
RewriteCond %{REQUEST_FILENAME}!webpage.htm
RewriteRule ^(.+)$ / [L]

jd01

7:09 pm on Oct 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteCond %{HTTP_REFERER} ^-?$
RewriteCond %{REQUEST_URI}!^/directory/
RewriteCond %{REQUEST_FILENAME}!webpage.htm
RewriteRule ^(.+)$ / [L]

I think you might be a little better off with something like this:

# Check to see if the referrer is empty:
RewriteCond %{HTTP_REFERER} !.
#check only requests for /directory/ OR webpage.htm
RewriteRule ^(webpage\.htm$)¦(directory/) http://www.yoursite.com/ [R=301,L]

The rule says if the requested location starts with /directory/ (implicit 'and everything else' without the $ ending) OR is webpage.htm start to finish, check the condition.

The condition says if the referrer does not contain a character, continue with the rule. To REDIRECT you will always want to use a full URL. Using only the / will REWRITE (serve the information from /) without redirecting the browser.

Failing to include the R=301 with your REDIRECT will cause a 302 (undefined redirect) header to be served.

() groupings on my rule may not necessarily be necessary.
(Been a while and I'm a little rusty, but this should get you close.)

Hope this helps.
Justin

wm2007

10:52 pm on Oct 8, 2006 (gmt 0)

10+ Year Member




GREAT

That was a well explained solution

Thanks a lot Justin, for helping me with this one :)