Forum Moderators: phranque

Message Too Old, No Replies

403 error for hotlink on any image extension

         

Asia_Expat

4:39 pm on May 30, 2008 (gmt 0)

10+ Year Member



I want to prevent hotlinking to any file on my server but still allow linking to my urls of course. Will this work and are there any caveats I should know about?...

RewriteEngine on
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com [NC]
RewriteRule .*\.(.*)$ - [F,NC]

encyclo

4:43 pm on May 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Without testing, I believe the above will block all external referrers - including visitors coming from a search engine or external link. I assume this would be an unintended consequence ;)

You have to specify file types for hotlink protection.

Asia_Expat

5:03 pm on May 30, 2008 (gmt 0)

10+ Year Member



Oh dear. I tried cobbling it together from old posts on webmasterworld and the code generated by cPanel.
Another problem I'm having is the fact that highlighting, pasting and posting from this forum is messing up the code and I don't know if it should be a pipe, a broken pipe, a question mark, or whatever.

jdMorgan

7:55 pm on May 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can't hotlink-protect "pages" -- only the objects (images, scripts, stylesheets) included on those pages. Otherwise, as encyclo states, your site will become inaccessible.

Jim

Asia_Expat

10:30 am on May 31, 2008 (gmt 0)

10+ Year Member



OK how does this look? (I know the double pipes should be solid)...

RewriteEngine on
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com [NC]
RewriteRule \.(jpe?g¦gif¦bmp¦png)$ - [NC,F]

jdMorgan

2:21 am on Jun 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looks OK, but you should not have to handle the "www or not-www" case, and you shouldn't have to handle upper/lowercase domain variations -- These should already have been taken care of by a domain canonicalization redirect.

RewriteEngine on
#
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://www\.example\.com
RewriteRule \.(jpe?g¦gif¦bmp¦png)$ - [NC,F]

Jim

Asia_Expat

11:27 am on Jun 1, 2008 (gmt 0)

10+ Year Member



Yeah, I do have the canonical issue taken care of in another area of the htaccess... thanks for pointing that out! :)