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

0

We were having issues with excessive bandwidth usage on our site cfts.co 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.

P
Posted 5 months agoby peter2cfu
#114323 viewsEdited 5 months ago

Please login to post an answer