How to block pictures, video or other files from direct URL Access via the .htaccess file? Open


We were having issues with excessive bandwidth usage on our site recently a lot of people were downloading .mp4 background from our site, consuming over 25GB bandwidth a month so very quickly we had to come up with a solution, after a lot of duckduck going (a google alternative) and a few mins of playing around we came up with the below, this has been working very well for us and have seen no issues to date.

Add the below text to your .httacces file

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^https://(www\.)?cfts\.co [NC]

RewriteCond %{HTTP_REFERER} !^https://(www\.)?cfts\.co.*$ [NC]
RewriteRule \.(txt|mp4)$ - [F]
# or you can use this for a more frendly denial.
# RewriteRule \.(txt|mp4)$ /messageforcurious [L]

This returns 403, if you access images directly, but allows them to be displayed on site.

You can change/add files extensions to exclude from direct URL access by changing this part, for example (txt|mp4|png|jpg|pdf)

If you're using a local web server for development etc. this may be more useful.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost.*$ [NC]
RewriteRule \.(gif|jpg)$ - [F]

Note: It is possible that when you open some pages with image and then copy that image's path into the address bar you can see that image, it is only because of the browser's cache, in fact, that image has not been loaded from the server.

Posted 1 year agoby peter2cfu
#1142485 viewsEdited 1 year ago

Please login to post an answer

Mon - Fri: 09:00-17:30 EAT
Weekends: Closed

Public Holidays: Closed

Uganda: +256-(0)392-733784
Uganda: +256-(0)414-533784
Rwanda: +250-(0)786-304817


Ours is a rich history providing holistic ICT support services; by designing, developing, implementing reliable and effective solutions, over the years we’ve learnt a thing or two about helping our customers getting IT right, the first time.

East Africa (Uganda, Rwanda, Kenya, Tanzania, Sudan) and DRC