How to deny access to specific folders on a server in Apache

You can deny access to certain folders using the following construction. You can add it in the Apache virtual host settings, httpd.conf or create a separate file with settings:

<DirectoryMatch "\.(git|svn|hg)">
  order allow,deny
  deny from all
</DirectoryMatch>

In this example, I am denying access to the .git, .svn and .hg folders. You can use other values ​​instead.

The same can be done with mod_rewrite:

Rewrite Engine on
RewriteCond %{REQUEST_URI} ^\.(git|svn|hg) -d
RewriteRule .* - [F]

This construction can be thrown into the Apache host settings and .htaccess file