Search code examples
bandwidth

is if ($referer != "mysite.com") not a good way to prevent other sites to hard link my images or swf?


sometimes i see an image not being served when the browser look at www.somesite.com/some_image.jpg -- it will say you need to look at the image from within a page.

(such as when using google's image search and looking at some results)

so i think their server is using something like

# pseudo code
if ($referer not contain "mywebsite.com") then not serve the image / swf

but this probably is not a good way since HTTP_REFERER is not reliable? so some users will end up not seeing the image or swf when referer info is missing?


Solution

  • or even better if you have access to using a .htaccess file you could do the following:

    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?example.com/.*$ [NC]
    RewriteRule \.(gif|jpg|js|css|cur|png|jpeg)$ - [F]
    

    or if you are wanting them to see a different image then do the following:

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