Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 368
  • Last Modified:

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
0
zattz
Asked:
zattz
  • 4
  • 4
1 Solution
 
joachim.claeys@teleatlas.comCommented:
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
 
zattzAuthor Commented:
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
 
joachim.claeys@teleatlas.comCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
zattzAuthor Commented:
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
 
joachim.claeys@teleatlas.comCommented:
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
 
zattzAuthor Commented:
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
 
joachim.claeys@teleatlas.comCommented:
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
 
zattzAuthor Commented:
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now