There are more than 150+ domains targeting my images & sending spammed links to my site. I just want to prevent hotlinking from them. I just want to block only those specific sites.
"150" is a little excessive to block specifically and this may become hard to maintain in the future. It would be more usual to block all hotlinking sites as in @closetnoc's answer, and just allow specific Referers as required.
However, to answer your question, if you want to block just those specific Referers then you would need something like the following.
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^https?://referer-1\.example/ [OR]
RewriteCond %{HTTP_REFERER} ^https?://referer-2\.example/ [OR]
# etc...
RewriteCond %{HTTP_REFERER} ^https?://referer-150\.example/
RewriteRule \.(jpe?g|png|gif)$ - [NC,F]
This would need to go at the top of your .htaccess
file.
There should be no OR
flag on the last condition, otherwise all traffic will be blocked.
The L
flag is not required on the RewriteRule
directive when using the F
flag, it is implied.
UPDATE: To make the above more efficient you should still first include the conditions that check that the Referer is not the expected hostname (and optionally is not blank) - as in @closetnoc's answer - so that the rule fails early for 99.9% of requests.
For example:
RewriteEngine On
# Fail early for all "direct" and "internal" requests
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?example\.com
# Block specific Referers...
RewriteCond %{HTTP_REFERER} ^https?://referer-1\.example/ [OR]
RewriteCond %{HTTP_REFERER} ^https?://referer-2\.example/ [OR]
# etc...
RewriteCond %{HTTP_REFERER} ^https?://referer-150\.example/
RewriteRule \.(jpe?g|png|gif)$ - [NC,F]