Link to home
Start Free TrialLog in
Avatar of zattz
zattz

asked on

HTACCESS redirect based on referer

This is what I want to do:

I have a domain: http://www.downhere.com

On that domain I will have a page that links to http://www.downhere.com/download.zip

download.zip does not exist, and will never exist.

If somebody tries to access download.zip (which dosnt exist) and they have a referer that matches my domain,
then I want them to be redirected to setup.zip

The end point is that I want anybody who tries to access download.zip without using a link on my site to get real 404 headers, but if they access the file from the link on my site I want them to be redirected to setup.zip

Thanks
Avatar of joachim.claeys@teleatlas.com
joachim.claeys@teleatlas.com
Flag of Belgium image

If you have mod_rewrite enabled on your webserver:

RewriteCond %{HTTP_referer} ^http://(www\.)?downhere\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) http://www.downhere.com/download.zip [R=301,L]

First line: check of the referer is your domain
Second line: check if the filename does not exists
Third line: Redirect to the download.zip file if above conditions are met

Avatar of zattz
zattz

ASKER

Umm,

Isnt the third line supposed to say setup.exe?

Also, I only want it to redirect if the file requested was download.zip, i don't want all files that don't exist to redirect to setup.exe
You mean to setup.zip.

Here's the corrected version:

RewriteCond %{HTTP_referer} ^http://(www\.)?downhere\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) http://www.downhere.com/setup.zip [R=301,L]

Avatar of zattz

ASKER

Yes I meant setup.zip :)

How come I don't see download.zip mentioned in any of the lines? I don't want all files that dont exist to be redirected?
RewriteCond %{HTTP_referer} ^http://(www\.)?downhere\.com [NC]
RewriteCond %{REQUEST_FILENAME} ^download.zip [NC]
RewriteRule (.*) http://www.downhere.com/setup.zip [R=301,L]

Untested ;-|

Avatar of zattz

ASKER

This works:

options all
RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_referer} ^http://(www\.)?downhere.com\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule download.zip http://downhere.com.com/setup.zip [R=301,L]

BUT i'm getting some strange behaviour.

When I visit the webpage and click the link, it downloads correctly.
But then if I try and access download.zip directly without clicking the link i get the 404 which I should.
But then if I got back to the page and click the link I get a 404!

But if I go to another computer with another IP and click the link there, it works, and then after doing it, the other pc works too! until it tries to access download.zip directly again!

any ideas?
ASKER CERTIFIED SOLUTION
Avatar of joachim.claeys@teleatlas.com
joachim.claeys@teleatlas.com
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zattz

ASKER

I fixed that before I use it... but the problem above is still there...

I have tried it with two webhosts... very weird.

I will accept your answer anyway, as its not your fault what I wanted to do dosnt work properly ;)

If you have any ideas please comment