Solved

HTACCESS redirect based on referer

Posted on 2006-11-23
8
355 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:zattz
  • 4
  • 4
8 Comments
 
LVL 3

Expert Comment

by:joachim.claeys@teleatlas.com
ID: 18003073
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

0
 

Author Comment

by:zattz
ID: 18003144
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
0
 
LVL 3

Expert Comment

by:joachim.claeys@teleatlas.com
ID: 18003267
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]

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:zattz
ID: 18003441
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?
0
 
LVL 3

Expert Comment

by:joachim.claeys@teleatlas.com
ID: 18003525
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 ;-|

0
 

Author Comment

by:zattz
ID: 18003609
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?
0
 
LVL 3

Accepted Solution

by:
joachim.claeys@teleatlas.com earned 500 total points
ID: 18003975
The last line contains an typing error ( the domainname ) :
RewriteRule download.zip http://downhere.com.com/setup.zip [R=301,L]

should probably be
RewriteRule download.zip http://www.downhere.com/setup.zip [R=301,L]

0
 

Author Comment

by:zattz
ID: 18003992
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
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question