Home: General Discussions: General Questions:
Rewrite Rule to block specific referer?



James
Troll

Aug 30, 2002, 9:57 AM


Views: 4936
Rewrite Rule to block specific referer?

This is an image-leeching-prevention question. I know I can use the code pasted below in my .htaccess file to prevent all external domains from leeching images.

This only allows image-linking from mydomain.com. Problem is I need to turn that around and deny image-linking from a single, specific domain (ebay.com), while allowing image-leeching from all other domains. We have affilliates who are authorized to "leech" banners. (Or maybe there's a way to exclude the banner directory from the rewrite rule above?)

Any ideas?


Code
 RewriteEngine on 
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.com/.*$ [NC]
RewriteRule \.(gif|jpg)$ - [F]




Jasmine
Administrator

Sep 15, 2002, 11:51 PM


Views: 4914
Re: [James] Rewrite Rule to block specific referer?

Have more fun than just making a broken image in ebay.com. Here's code that will put an image of your choosing in there instead. Sly


Code
RewriteEngine on  
RewriteCond %{HTTP_REFERER} ^http://(www\.)?ebay.com/.*$
RewriteRule \.(gif|jpg)$ /stopstealing.gif [NC]


Or if you needed to deny multiple domains, you can use [url=http://httpd.apache.org/docs/mod/mod_rewrite.html#RewriteCond][OR]:


Code
RewriteEngine on  
RewriteCond %{HTTP_REFERER} ^http://(www\.)?ebay.com/.*$ [OR]
RewriteCond %{HTTP_REFERER} ^http://dummy.hypermart.com/.*$
RewriteRule \.(gif|jpg)$ /stopstealing.gif [NC]


Note that the RewriteCond and RewriteRule uses regular expressions, and that the period means "any single character". So while the period between ebay and com will match as intended (because the way the rest of the expression is structured), it's important to realize that the . is actually a regular expression metacharacter that will match a single character. In this case, it'll match the period.