Solved

HTACCESS redirect based on referer

Posted on 2006-11-23
8
353 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
 

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

705 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now