Deny access to one specific folder in .htaccess

Apache.Htaccess

Apache Problem Overview


I'm trying to deny users from accessing the site/includes folder by manipulating the URL.

I don't know if I have to deny everything and manually make individual exceptions to allow, if I can just deny this one folder, or if there's a rewrite function that can be used.

Specific example: I don't want to see the directory files by typing in localhost/site/includes into the URL.

Apache Solutions


Solution 1 - Apache

Create site/includes/.htaccess file and add this line:

Deny from all

Solution 2 - Apache

You can also deny access to a folder using RedirectMatch

Add the following line to htaccess

RedirectMatch 403 ^/folder/?$

This will return a 403 forbidden error for the folder ie : http://example.com/folder/ but it doest block access to files and folders inside that folder, if you want to block everything inside the folder then just change the regex pattern to ^/folder/.*$ .

Another option is mod-rewrite If url-rewrting-module is enabled you can use something like the following in root/.htaccss :

RewriteEngine on

RewriteRule ^folder/?$ - [F,L]

This will internally map a request for the folder to forbidden error page.

Solution 3 - Apache

In an .htaccess file you need to use

Deny from  all

Put this in site/includes/.htaccess to make it specific to the includes directory

If you just wish to disallow a listing of directory files you can use

Options -Indexes 

Solution 4 - Apache

We will set the directory to be very secure, denying access for all file types. Below is the code you want to insert into the .htaccess file.

Order Allow,Deny 
Deny from all 

Since we have now set the security, we now want to allow access to our desired file types. To do that, add the code below to the .htaccess file under the security code you just inserted.

<FilesMatch "\.(jpg|gif|png|php)$">
Order Deny,Allow
   Allow from all
</FilesMatch>

your final .htaccess file will look like

Order Allow,Deny 
Deny from all 

<FilesMatch "\.(jpg|gif|png|php)$">
Order Deny,Allow
   Allow from all
</FilesMatch>

Source from Allow access to specific file types in a protected directory

Solution 5 - Apache

You can create a .htaccess file for the folder, wich should have denied access with

Deny from  all

or you can redirect to a custom 404 page

Redirect /includes/ 404.html

Solution 6 - Apache

Just put .htaccess into the folder you want to restrict

## no access to this folder

# Apache 2.4
<IfModule mod_authz_core.c>
	Require all denied
</IfModule>

# Apache 2.2
<IfModule !mod_authz_core.c>
	Order Allow,Deny
	Deny from all
</IfModule>

Source: MantisBT sources.

Solution 7 - Apache

Creating index.php, index.html, index.htm is not secure. Becuse, anyone can get access on your files within specified directory by guessing files name. E.g.: http://yoursite.com/includes/file.dat So, recommended method is creating a .htaccess file to deny all visitors ;). Have fun !!

Solution 8 - Apache

You can also put this IndexIgnore * at your root .htaccess file to disable file listing of all of your website directories including sub-dir

Solution 9 - Apache

On Apache 2.4 you can use an Apache <If> expression in the root .htaccess file to block direct access to this specific subdirectory and everything within it.

For example:

<If "%{REQUEST_URI} =~ m#^/site/includes($|/)#">
    Require all denied
</If>

Solution 10 - Apache

You can do this dynamically that way:

mkdir($dirname);
@touch($dirname . "/.htaccess");
  $f = fopen($dirname . "/.htaccess", "w");
  fwrite($f, "deny from all");
fclose($f);

Solution 11 - Apache

For some reasons which I did not understand, creating folder/.htaccess and adding Deny from All failed to work for me. I don't know why, it seemed simple but didn't work, adding RedirectMatch 403 ^/folder/.*$ to the root htaccess worked instead.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2835191View Question on Stackoverflow
Solution 1 - ApacheanubhavaView Answer on Stackoverflow
Solution 2 - ApacheAmit VermaView Answer on Stackoverflow
Solution 3 - ApacheAndyView Answer on Stackoverflow
Solution 4 - ApachearthankamalView Answer on Stackoverflow
Solution 5 - ApacheMarvinView Answer on Stackoverflow
Solution 6 - ApacheThe GodfatherView Answer on Stackoverflow
Solution 7 - ApacheMerajView Answer on Stackoverflow
Solution 8 - ApacheRavi SoniView Answer on Stackoverflow
Solution 9 - ApacheMrWhiteView Answer on Stackoverflow
Solution 10 - ApacheRatajSView Answer on Stackoverflow
Solution 11 - ApacheChimdiView Answer on Stackoverflow