'403 access denied' error to 'custom folders' in Drupal after enabling mod_rewrite.

Recently I encountered another problem when adding a custom sub-folder to a standard Drupal 7/8 installation:

If however for some reason you still get the '403 access denied' error when trying to access that directory then add the following lines to your .htaccess:

<IfModule mod_rewrite.c>
     RewriteEngine on
     # stuff to let through (ignore)
     RewriteCond %{REQUEST_URI} "/<name of your sub-folder>/"
     RewriteRule (.*) $1 [L]
</IfModule>

This worked for me on an installation of Drupal 7/8

In Drupal 9 however the text as listed above should be written in a separate .htaccess file which should be placed inside the requested subfolder.

 

Block access to "hidden" directories whose names begin with a period.

Files whose names begin with a period, as well as the control files used by CVS, are protected by the FilesMatch directive.
This includes directories used by version control systems such as Subversion or Git to store control files.

 

<IfModule mod_rewrite.c>
      RewriteRule "/\.|^\.(?!well-known/)" - [F]
</IfModule>

 

NOTE: This only works when mod_rewrite is loaded. Without mod_rewrite, it is not possible to block access to entire directories from .htaccess because <DirectoryMatch> is not allowed here.
If you do not have mod_rewrite installed, you should remove these directories from your webroot or otherwise protect them from being downloaded.