在DirectoryMatch中使用FilesMatch进行Apache基本身份验证

时间:2012-03-28 13:55:22

标签: .htaccess apache2

我想限制对某些特定PDF文件的访问,但我的Apache配置遇到了一些麻烦。我已经使FilesMatch指令工作如下:

<FilesMatch "\.pdf$">
   AuthName "Permission Required"
   AuthUserFile /path/to/.htpasswd"
   Require valid-user
   AuthType Basic
</FilesMatch>

现在我明白我只能在我的.htaccess文件中更具体,因此我必须在配置文件中使用LocationMatch或DirectoryMatch。我尝试了以下内容,但没有成功:

<VirtualHost *:80>
   DocumentRoot "/var/www/somedomain.com/httpdocs"
   ServerName somedomain.com:80
   ServerAlias somedomain.com
   <DirectoryMatch "^/uploads/(dir1|dir2)">
      <FilesMatch "\.pdf$">
         AuthName "Permission Required"
         AuthUserFile "/var/www/somedomain.com/bin/.htpasswd"
         Require valid-user
         AuthType Basic
      </FilesMatch>
   </DirectoryMatch>
</VirtualHost>

实际文件位于:http://www.somedomain.com/uploads/dir1/somefile.pdf

我确信这很简单,但我无法让它正常工作。

1 个答案:

答案 0 :(得分:2)

<VirtualHost *:80>
   DocumentRoot "/var/www/somedomain.com/httpdocs"
   ServerName somedomain.com:80
   ServerAlias somedomain.com
   <DirectoryMatch "^/var/www/somedomain.com/httpdocs/uploads/(dir1|dir2)">
      <FilesMatch "\.pdf$">
         AuthName "Permission Required"
         AuthUserFile "/var/www/somedomain.com/bin/.htpasswd"
         Require valid-user
         AuthType Basic
      </FilesMatch>
   </DirectoryMatch>
</VirtualHost>

我发现了什么是错的。对不起我起初没注意:)。 Probelm DirectoryMatchDirectory完全相同,但它以正则表达式作为参数。因此,当您的文档根目录为/var/www/somedomain.com/httpdocs时,上传目录应该像这样指示

<DirectoryMatch "^/var/www/somedomain.com/httpdocs/uploads/(dir1|dir2)">
      #Somemthibg goes here :-D
 </DirectoryMatch>