Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess deny files in subfolders

I want to block access to "sub/folder/index.php", but not "index.php" anywhere else.

<Files index.php>
    Order allow,deny
    Deny from all
</Files>

works but blocks all "index.php" files.

<Files sub/folder/index.php>
    Order allow,deny
    Deny from all
</Files>

doesn't work. Do I need to use <Directory>?

I don't want to create an .htaccess file in the sub/folder

I don't have access to httpd.conf

I don't want to block everything in sub/directory

If it were that easy, I wouldn't be asking ;)

like image 388
Halcyon Avatar asked Jan 10 '12 10:01

Halcyon


People also ask

Does htaccess work on subdirectories?

You can add a . htaccess file to any directory that requires lenient permissions settings (such as 760, 766, 775 or 777). You can prevent the execution of scripts inside the directory and all its sub-directories.

How to deny access to a folder htaccess?

Create a . htaccess file in the root of your project directory structure. Then open the . htaccess file and write this directive deny from all.

What does htaccess Deny from all do?

htaccess to make it happen. For example, deny from all is a command that will allow you to apply access restrictions to your site.


1 Answers

I think the simpliest rule is:

RedirectMatch 403 ^.*/sub/folder/index\.php$

RedirectMatch is in mod_alias module, that you certainly have. This does not implies mod_rewrite engine. Here we are simply telling apache that any access to sub/folder/index.php will generate a "403 forbidden" answer.

You can put that in a httpd.conf or in the root .htaccess (but really consider removing all .htaccess, it's bad, it's slow, it's sad that you do not have access to the real configuration files).

like image 107
regilero Avatar answered Oct 24 '22 09:10

regilero