(htaccess) How to make a file only accessable by host Server and prevent direct access? (htaccess) How to make a file only accessable by host Server and prevent direct access? apache apache

(htaccess) How to make a file only accessable by host Server and prevent direct access?


This is called "hotlinking" - I suggest reading up on it first, as there is more to it than just a line or to of code in your .htaccess. This page is decent: http://altlab.com/htaccess_tutorial.html

RewriteEngine OnRewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]RewriteCond %{HTTP_REFERER} !^$RewriteRule .*\.(jpe?g|gif|bmp|png)$ http://i.imgur.com/qX4w7.gif [L]

This will show an image from imgur, however I sometimes like to go further and host alternative images from my own host - if it's a competitor, for instance, you can have a lot of fun ;)


There is a LOT more going on to prevent hotlinking of images, than an .htaccess rule. However, the basis of what you'd want in your htaccess to prevent image hotlinking is :

RewriteCond %{HTTP_REFERER} !^http://(www\.)?example.com/.*$      [NC]RewriteRule .*\.(jpg|jpeg|png|bmp)$ - [F,NC]

Apache ReWrite guide


Not entirely sure but this should guide you:

RewriteEngine onRewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]RewriteRule (images/sample.jpg)$ - [R=404,L]