For those looking to simply deny all "hidden" files and directories on a Linux distribution (generally all files beginning with a "."), here's what works on Apache 2.4 when placed in server conf context:
<FilesMatch "^\.(.*)$">
Require all denied
</FilesMatch>
<DirectoryMatch "/\.(.*)">
Require all denied
</DirectoryMatch>
And here's the older Apache 2.2 style (same regex, just different auth directives):
<FilesMatch "^\.(.*)$">
Order deny,allow
Deny from all
</FilesMatch>
<DirectoryMatch "/\.(.*)">
Order deny,allow
Deny from all
</DirectoryMatch>
Then you don't have to worry about .git
or .svn
specifically. That would also match things like .htaccess
and .htpasswd
inherently.
Personally, I like issuing 403s for such requests instead of 404s, but you could easily use a RewriteRule instead of auth denial, like so:
<FilesMatch "^\.(.*)$">
RewriteRule "^(.*)$" - [R=404,L]
</FilesMatch>
<DirectoryMatch "/\.(.*)">
RewriteRule "^(.*)$" - [R=404,L]
</DirectoryMatch>