Question

hello all i am working on a site where i dont want any user to browse my site directory or to view any image/file inside the directory by just typing the address of the image or file in address bar or by any software help.

i think this is possible my .htaccess rewrite rule can anyone help me please my site directory names are members,images,ads from some where on this site i found this

  RewriteCond %{REQUEST_URI} ^/news/latestnews/(.*)$
  RewriteCond %$1 ^(.*)2012(.*)$
  RewriteRule ^(.*)$ /news/2012 [R=301,L]

which redirects the user to new directory but i dont know how to work with htaccess i can not modify the code

Was it helpful?

Solution

You can add following to top of the htaccess file you already have

Options -Indexes

RewriteEngine on 

RewriteCond %{HTTP_REFERER} !^http://(www\.)?YOURDOMAINNAME.*$ [NC] 
RewriteRule \.(gif|jpg|png)$ http://www.YOURDOMAINNAME.com [L]

Options -Indexes will restrict directory listing

RewriteCond %{HTTP_REFERER} !^http://(www\.)?YOURDOMAINNAME.*$ [NC] checks if the request to a file is from within script or if the request has a referrer from your domain

RewriteRule \.(gif|jpg|png)$ http://www.YOURDOMAINNAME.com [L] will redirect to your home page, if some one tries to access the file directly. You can added file extension to this rule by separating it by pipe | symbol, e.g. gif|jpg|png|doc|pdf

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top