Apache configuration: Regex to disable access to files/directories beginning with a dot

I want to disable access to any file OR directory, whose name begins with a DOT. I came up with the following but, it disables access to files/directories beginning with DOT only if they are directly in the Document root.

<Files ~ "^\.|\/\.">
    Order allow,deny
    Deny from all
</Files>

With this,

http://my_server.com/.svn/entries   --> Permission denied
http://my_server.com/abcd/.svn/entries  --> Accessible, should be disabled

Whats the proper regex to achieve this?

Thanks,

JP

Answers


You code does not work because <Files> only applies to the basename of the requested document. That is, the part after the last slash. (source: http://httpd.apache.org/docs/current/mod/core.html#files)

Apache blocks .htaccess files in a default installation (better: all files starting with .ht). If you looked closely at the configuration files, you would see something like this:

<FilesMatch "^\.ht">
    Order allow,deny
    Deny from all
</FilesMatch>

So to hide all files starting with a dot, you would use:

<FilesMatch "^\.">
    Order allow,deny
    Deny from all
</FilesMatch>

In order to make it work for directories starting with a dot, use the following (tested) code:

<DirectoryMatch "^\.|\/\.">
    Order allow,deny
    Deny from all
</DirectoryMatch>

Need Your Help

Deploy Rails 3 site using AWS

ruby-on-rails-3.1 amazon-web-services

I am going to be deploying a site i have built in rails 3 for the first time.. It has been recommended that I use AWS. I have read through some tutorials online but keep running into the same probl...

printing to a file in Python: redirect vs print's file argument vs write

python file redirect python-3.x stdout

I have a bunch of print calls that I need to write to a file instead of stdout. (I don't need stdout at all.)

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.