How to prevent external direct-link to files with htaccess

.htaccessbandwidth

I have this in my htaccess at the public_html folder of my file server:

RewriteEngine On
RewriteBase /
ReWriteCond %{REQUEST_URI} ^/files/*
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite.net/.*$ [NC]
RewriteRule \.(gif|png|jpg|jar|sis|mp3|thm)$ - [F]

And I have lots of folders and files inside the mysite.net/files dir, which I want to prevent from being direct-downloaded from sites other than mysite.net. However the above rules don't seem to work despite I know that .htaccess file is enabled. In some cases it only blocks the images but no the files. Please tell me what's wrong with that script, or if I can use any other directive to prevent external sites from leeching my files constantly. Thanks

Best Answer

Does it help if you add ".*"?

RewriteRule .*\.(gif|png|jpg|jar|sis|mp3|thm)$ - [F]

Also, your line:

ReWriteCond %{REQUEST_URI} ^/files/*

means (at the end) "a slash zero or more times". You might try:

ReWriteCond %{REQUEST_URI} ^/files/.*

which means "a slash then any character zero or more times".

Edit: Some of your files may be named with uppercase extensions. You might try:

RewriteRule .*\.(gif|png|jpg|jar|sis|mp3|thm)$ - [F,NC]