April 29, 2025 - 18:41
Hotlink Protection (with .htaccess) Image
Server Management

Hotlink Protection (with .htaccess)

Comments

Hotlink Protection via .htaccess

Hotlink protection is a method used to prevent other websites from directly accessing your files such as png, jpg, gif, pdf, css, js, etc., hosted on your server. It’s very easy to implement.

Simply open your .htaccess file and add the following lines at the bottom:

GENEL
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain.com/.*$ [NC]
RewriteRule \.(jpg|jpeg|gif|png|bmp|pdf|css|js)$ - [F]

You can allow multiple valid referers like this:

GENEL
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://yourdomain.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^https://yourdomain.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.yourdomain.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^https://www.yourdomain.com/.*$ [NC]
RewriteRule \.(jpg|jpeg|gif|png|bmp|pdf|css|js)$ - [F,NC]

If you prefer to redirect users who hotlink to a specific URL (like your homepage), modify the last line as follows:

GENEL
RewriteRule \.(jpg|jpeg|gif|png|bmp|pdf)$ https://www.yourdomain.com [R,NC]

Or, if you want to display a specific image (e.g., a warning banner) instead of the original hotlinked image, use:

GENEL
RewriteRule \.(gif|jpg|png)$ https://www.yourdomain.com/blocked.png [R,L]

That’s all for hotlink protection. Feel free to reach out if you have any questions!

Related Articles

Comments ()

No comments yet. Be the first to comment!

Leave a Comment