Block direct access to files and directories

Asked

Viewed 14,408 times

10

Well, my doubt is simple:

How can I block direct access to files and directories on my website? For example, prevent the user from accessing the link: www.meusite.com/img and get the list of images, or /js and get the JS files, and so on.

Today I own a file index blank in each folder, which "blocks" the access, or rather, the view.

I tried to use the htaccess to create this lock, something like that:

<Directory  ~ "\">
    Order allow,deny
    Deny from all
</Directory>

However I am getting total block, not even browsing the site is allowed. The only other configuration I have in my htaccess is the rewrite to improve the url site. How I am working with the routes in AngularJs, the code below serves to remove the /#/ that is added.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index.html$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.html [L]
</IfModule>

How can I get the result? Because I have already visualized some websites that when accessing the folder, we get the result of "Forbiden access" which, I believe, is the most correct process to be done.

Or... What is the real purpose/logic I should have in this regard?

1 answer

9


The option you seek is this:

Options -Indexes 

Can you direct us .htaccess (if this override is enabled in Apache).

Example of configuration with Directory, instead of .htaccess:

<Directory /caminho> 
    Options -Indexes 
</Directory>

Documentation (en):

https://httpd.apache.org/docs/2/sections.html#file-and-web

  • In /caminho what should I specify? My htaccess file is at the root of the server /public_html and it needs to be there for the rewrite with Angularjs to work. I tried naming some folders but all return me with error 500 Internal Server Error

  • no. htaccess does not need path, it is only the Options -Indexes same, the only way is to use in apache config

  • The yes, perfect one! The solutions I found were always very complex, with the name of the directories and files that we wanted to block or allow access and always returned some error or blocked full access. I didn’t think it would be so simple. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.