Forum Moderators: phranque

Message Too Old, No Replies

How do I get this to work -- htaccess

         

dantra

5:32 pm on Nov 19, 2006 (gmt 0)

10+ Year Member



After trying many different ways of preventing hot-linking to my images, I finally got one to work:

RewriteEngine on
# Prevent hot-linking
# Allow linking from my own site
RewriteCond %{HTTP_REFERER}!^http://mysite.com/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://mysite.com$ [NC]
RewriteCond %{HTTP_REFERER}!^http://www.mysite.com/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://www.mysite.com$ [NC]
# But not allow hot-linking to any images in the image_folder
RewriteCond %{REQUEST_URI}!^/image_folder/.*$ [NC]
# Re-direct any links to image files to another site
RewriteRule .*\.(jpg¦jpeg¦gif¦png¦bmp)$ [site-to-redirect-to.com...] [R,NC,L]

I’m trying to apply the Rewrite rule in the .htaccess to affect the rest of my site, not just the home page. Will this work?

RewriteEngine on
# Prevent hot-linking
# Allow linking from my own site
RewriteCond %{HTTP_REFERER}!^http://mysite.com/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://mysite.com$ [NC]
RewriteCond %{HTTP_REFERER}!^http://www.mysite.com/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://www.mysite.com$ [NC]
RewriteCond %{HTTP_REFERER}!^http://mysite.com/page2/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://mysite.com/page2$ [NC]
RewriteCond %{HTTP_REFERER}!^http://www.mysite.com/page2/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://www.mysite.com/page2$ [NC]
RewriteCond %{HTTP_REFERER}!^http://mysite.com/page3/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://mysite.com/page3$ [NC]
RewriteCond %{HTTP_REFERER}!^http://www.mysite.com/page3/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://www.mysite.com/page3$ [NC]
etc…
# But not allow hot-linking to any images in the image_folder
RewriteCond %{REQUEST_URI}!^/image_folder/.*$ [NC]
RewriteCond %{REQUEST_URI}!^/image_folder2/.*$ [NC]
RewriteCond %{REQUEST_URI}!^/image_folder3/.*$ [NC]
etc…
# Re-direct any links to image files to another site
RewriteRule .*\.(jpg¦jpeg¦gif¦png¦bmp)$ [site-to-redirect-to.com...] [R,NC,L]

I have a total of seven pages and image folders that I would like to add, I would also like to mention that [mysite.com...] and [mysite.com...] etc... ends with a .htm like this [mysite.com...] Does that make a difference?

jdMorgan

5:57 pm on Nov 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The following should be entirely sufficient to block hotlinking on your entire site:

RewriteEngine on
# Prevent hot-linking
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mysite\.com [NC]
# Re-direct any links to image files to another site
RewriteRule \.(jpe?g¦gif¦png¦bmp)$ http://www.site-to-redirect-to.com/ [R,NC,L]

However, that code will prevent any image viewing by visitors whose browsers do not send an HTTP Referer header, or whose access to the Web is through any kind of firewall --software or hardware, local, ISP, or corporate-- that blocks the transmission of a referrer header, or through a caching proxy such as used by EarthLink, AOL, and many other ISPs. In this case, your site will appear to be broken to the visitor.

The fact is that you cannot rely on the HTTP Referer header to be present or to be correct. It will be blocked without the knowledge of many visitors, and is easy enough to spoof as well.

Therefore, most Webmasters find that it is necessary to allow blank referrers as a trade-off between protecting their site and not having their site appear to be broken to the 10% or so of their visitors who don't provide a referrer.

Also, it is a waste of internet resources to redirect hotlinkers elsewhere -- You are using everyone's network bandwidth and simply passing the problem along to another Webmaster to suffer with. I recommend that as a citizen of the Web, you simply block the access with a 403-Forbidden response and be done with it.


RewriteEngine on
# If HTTP_REFERER header is present (non-blank)
RewriteCond %{HTTP_REFERER} .
# and if referrer is NOT my own site
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mysite\.com [NC]
# Forbid access
RewriteRule \.(jpe?g¦gif¦png¦bmp)$ - [F] [NC,L]

I removed many RewriteConds and optimized-out some unnecessary regex. If your goal is to block hotlinking of any image on your site, then none of that was needed.

Jim

[edited by: jdMorgan at 5:57 pm (utc) on Nov. 19, 2006]