Forum Moderators: phranque
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?
:)
{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.
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]
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