1

I saw Google Images seems to index my images only if hotlink protection is off. [*]

I use anyway hotlink protection because I don't like the idea of people sucking my bandwidth, i simply use this code to protcet my images from being hotlinked:

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

But in order to allow Google Image search to bypass my hotlink protection (I want Google Images search to show my images) would it suffice to add these lines:

RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google\.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google\.com$ [NC]

Because I'm wondering:

  • are the images hotlinked just from www.google.com?
  • and what about google.it / google.co.uk, etc.?

FYI: on Google official guidelines I did not find info about this.


[*] I suppose hotlink protection prevents Google Images to show images in its results because I did some tests and it seems hotlink protection does prevent my images to be shown in Google Images search.

3 Answers 3

1

google.com, google.co.uk, etc. are different domains and, therefor, if you only allow one then the others will be blocked.

So you have two options. You could write out every Google variant (and Bing variant if you wanted images in their results too, and for other search engines) as conditionals - but that is quite impractical.

Another option is that you redirect all traffic that is attempting to access images from an external website to a server side script. In that script you can then do various things to check the referer (i.e. check that the referer includes Google at the start of the URL, etc.). If the referer is from an allowed domain, then the page would serve the image to the client using the appropriate header (e.g. header("Content-type: image/jpeg"); ) and then reading the image contents.

2
  • "redirect to the image" - I don't think you mean redirect? If you did redirect then it doesn't come from within your site, but from the source of the initial HTTP request and the same referer is sent. I would have thought your server-side script should read the appropriate image and serve it to the client in the same request, and this bypasses .htaccess (since it's not HTTP).
    – MrWhite
    Dec 4, 2012 at 18:42
  • @w3d Valid comment, I'll modify my answer.
    – user18960
    Dec 4, 2012 at 19:55
1

This is the code you need

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?your-domain.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?google\..+$ [NC]
RewriteRule \.(gif|jpg|jpeg|png)$ https://your-domain.com/hotlink.jpg [R,L]

This code loads hotlink.jpg for sites hotlinking your images, Except for google.??? domains

1
0

Wouldn't this work? !^http(s)?://(www\.)?google\.*$/.*$

2
  • 1
    Maybe add a bit more clarification could be added to this answer as to what exactly you mean. Aug 12, 2015 at 14:49
  • @Joan Manuel Hernandez: yes and no, it works, but with your code any host with domains like "googlefake.com", "google123.es", etc. would be allowed to go through my hotlink protection, making hotlink protection useless. Aug 12, 2015 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.