开发者

*nix hosting - redirect download to homepage if referrer doesn't match

开发者 https://www.devze.com 2022-12-21 15:26 出处:网络
I have a software package placed in the root directory of my website. I\'d like all visitors which come from other websites to see the homepage first, befo开发者_运维技巧re they download. How can I do

I have a software package placed in the root directory of my website. I'd like all visitors which come from other websites to see the homepage first, befo开发者_运维技巧re they download. How can I do that without using a script?


Apache's mod_rewrite can do this. It can test 'HTTP_REFERER' variable and create a redirect.

Example from http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

Description:

Assume we have under http://www.quux-corp.de/~quux/ some pages with inlined GIF graphics. These graphics are nice, so others directly incorporate them via hyperlinks to their pages. We don't like this practice because it adds useless traffic to our server. Solution:

While we cannot 100% protect the images from inclusion, we can at least restrict the cases where the browser sends a HTTP Referer header.

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://www.quux-corp.de/~quux/.*$ [NC]
RewriteRule .*\.gif$        -                                    [F]

RewriteCond %{HTTP_REFERER}         !^$
RewriteCond %{HTTP_REFERER}         !.*/foo-with-gif\.html$
RewriteRule ^inlined-in-foo\.gif$   -                        [F]

Other variant http://articles.sitepoint.com/print/apache-mod_rewrite-examples

RewriteCond %{HTTP_REFERER} !^$  
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com/.*$ [NC]  
RewriteRule \.(gif|jpg|png)$ http://www.example.com/hotlinked.gif [R=301,L]

First line checks if referer is empty. second - if it is not your domain. Third will redirect "bad queries" to "hotlinked.gif"

0

精彩评论

暂无评论...
验证码 换一张
取 消