Forum Moderators: phranque

Message Too Old, No Replies

Hot Link Protect error

prevents images without www

         

cyberdyne

1:39 pm on Oct 13, 2010 (gmt 0)

10+ Year Member



Hi,
My HotLink protection works fine - or at least so I thought.
I today noticed that while it is working to prevent HotLinking from domains other than my own, it is also preventing images from being displayed when 'www' is
    not
used in the url.

I assume I need to add '(www\.)?' into what I already have but am unsure on both how to incorporate it and whether the '([a-z0-9\-]+)\.' which I already have is necessary so any help would be greatly appreciated.

Thank you in advance.

My code is as follows:
RewriteCond %{HTTP_REFERER} !^(http://([a-z0-9\-]+)\.domain\.co\.uk.*)?$ [NC]
RewriteCond %{HTTP_REFERER} !^(http://([a-z0-9\-]+)\.domain\.co\.uk.*)?$ [NC]
RewriteCond %{HTTP_REFERER} !^(http://([a-z0-9\-]+)\.subdomain\.domain\.co\.uk.*)?$ [NC]
RewriteCond %{HTTP_REFERER} !^(http://facebook\.com.*)?$ [NC]
RewriteCond %{HTTP_REFERER} !^(http://google\.com.*)?$ [NC]
RewriteRule \.(jpe?g|gif|bmp|png|psd)$ http://image.com/image.png [R=302,L]

cyberdyne

1:49 pm on Oct 13, 2010 (gmt 0)

10+ Year Member



OK, playing about with it, the following seems to have done the trick, but does removing '([a-z0-9\-]+)\.' have any adverse affect?

Thanks


RewriteCond %{HTTP_REFERER} !^(http://(www\.)?domain1\.co\.uk.*)?$ [NC]
RewriteCond %{HTTP_REFERER} !^(http://(www\.)?domain2\.co\.uk.*)?$ [NC]
RewriteCond %{HTTP_REFERER} !^(http://(www\.)?subdomain\.domain\.co\.uk.*)?$ [NC]
RewriteCond %{HTTP_REFERER} !^(http://(www\.)?facebook\.com.*)?$ [NC]
RewriteCond %{HTTP_REFERER} !^(http://(www\.)?google\.com.*)?$ [NC]
RewriteRule \.(jpe?g|gif|bmp|png|psd)$ [image.com...] [R=302,L]

jdMorgan

2:54 pm on Oct 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The question is really "what does the subpattern ([^a-z0-9\-]+)\." mean.

In this application, it means "any subdomain" and so would match "www" if coded properly.

You can modify it to match any and all subdomains (and sub-sub-domains) and (if "domain1" or "domain2" is the smae as "domain" in your example) to use one less RewriteCond in the process:

RewriteCond %{HTTP_REFERER} !^(http://([a-z0-9\-]+\.)*domain\.co\.uk.*)?$ [NC]
RewriteCond %{HTTP_REFERER} !^http://([a-z0-9\-]+\.)*domain1\.co\.uk [NC]
RewriteCond %{HTTP_REFERER} !^http://([a-z0-9\-]+\.)*domain2\.co\.uk [NC]
RewriteCond %{HTTP_REFERER} !^http://([a-z0-9\-]+\.)*facebook\.com [NC]
RewriteCond %{HTTP_REFERER} !^http://([a-z0-9\-]+\.)*google\.com [NC]
RewriteRule \.(jpe?g|gif|bmp|png|psd)$ http://image.com/image.png [R=302,L]

Because the pattern now matches any number of subdomains in the referring hostname, it is no longer necessary to specify any specific subdomain label for any "allowed" referrer.

I note that you are redirecting to an image on an "outside domain" -- probably not a good idea, since it makes you a hotlinker yourself. If this was done as an "emergency fix" to stop an infinite redirection loop, be aware the you could have fixed that problem simply by excluding the replacement image's URL-path from your rule using

RewriteCond %{REQUEST_URI} !^/image\.png$

and thus allowing the replacement image to be fetched.

I'd also suggest the use of a more-widely-supported format for that replacement image, as many older browsers did not have PNG support. Either .gif or .jpg would be better.

See this concurrent thread for more info: [webmasterworld.com...]

Jim

cyberdyne

3:14 pm on Oct 13, 2010 (gmt 0)

10+ Year Member



Many thanks Jim.

I've altered the subpattern as you suggested and it works perfectly. I've also take your advice and used an image hosted at my domain, inserting the exclusion (although the previous domain was just an free image host).

Many thanks as always.

cyberdyne

3:14 pm on Oct 13, 2010 (gmt 0)

10+ Year Member



Oh, I've also changed the .png to a .gif :)
Thank you.