Latest Greatest WordPress anti-hotlinking .htaccess solution
So I’ve had some problem with file leechers. I made fun of them, renamed the most popular images, and implemented the supposed ultimate .htaccess anti-hotlinking solution. Alas it didn’t work for my blog as I use a subdomain and I think my webhost uses some symbolic links to make this happen. My webhost uses DirectAdmin, there are some anti-hotlinking solutions out there specific to webhosts and website control panels.
Some question the wisdom of preventing hotlinking, they obviously haven’t had half of their bandwidth taken up by it. I’m still investigating how to allow the various search engines and feed readers to access the images. I had a more elaborate solution, but like I said it didn’t work with my blog subdomain. It did work on my other subdomains however.
The third feature/plan was to allow me to post links to certain images on forums myself. These images were only used on these forums and I put them in their own subdirectory. This was actually fairly easy to make work.
So what do you need to do to replicated my solution? Well first it takes three .htaccess files. One goes in your root directory where your index file is. On my webhost that directory is called public_html. You also need another .htaccess file to go in the directory you want to allow hotlinking to. Finally I needed to modify the existing .htaccess file in my blog subdomain directory.
All three of these files are called .htaccess and rely on mod_rewrite and probably only work on the apache webserver. So check with your webhost to make sure this solution will work and change the domain from Muschamp.ca to whatever yours is. I never could get the alternate image replacement stuff to work. I may keep working on it, or I may spend my efforts verifying that feed readers and search engines can get through as I get a lot of traffic from Google Image Search.
Main .htaccess file
# ultimate hotlink protection
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g?|png)$ [NC]
RewriteCond %{HTTP_REFERER} !^https?://([^.]+\.)?muschamp\. [NC]
RewriteRule \.(gif|jpe?g?|png)$ - [F,NC,L]
</ifModule>
Allowing hotlinking to images in a specific directory .htaccess file
# disable hotlink protection
RewriteEngine off
blog WordPress subdomain subdirectory .htaccess file
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Options +FollowSymlinks
# Protect Hotlinking
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g?|png)$ [NC]
RewriteCond %{HTTP_REFERER} !^https?://([^.]+\.)?muschamp\. [NC]
RewriteRule \.(gif|jpe?g?|png)$ - [F,NC,L]
Perhaps there should be an if statement around the second batch of rules but it works like this so I decided to document it.
Related posts:
- Time for a .htaccess anti-hotlinking solution
- A day later and a few dollars shorter
- Fixing Mint was easy
- Before you upgrade WordPress
This entry was originaly posted on , it was last edited on and is filed under: Blogging, Internet, WordPress and tagged: apache, blog subdomain, file leeches, hotlinking, htaccess, mod_rewrite.
Pingback: Calf Crazy – Muskblog
Pingback: Installed the Facebook for WordPress plugin – Muskblog
Pingback: You have gotta see this photo! – Muskblog
Pingback: My ten most useful blog postings – Muskblog
Pingback: Spammers, Scammers, and other Internet Assholes – Muskblog