1

I'd like to limit access to some specific PDF files but am having a bit of trouble with my Apache configuration. I've got the FilesMatch directive to work as follows:

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

Now I understand that I can't be more specific in just my .htaccess file, and so I'd have to use LocationMatch or DirectoryMatch in my config file. I've tried the following, but with no success:

<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>

The actual files are located: http://www.somedomain.com/uploads/dir1/somefile.pdf

I'm sure it's something simple, but I just can't get it to work correctly.

2
  • you opened FilesMatch and didn't close it (this applies to DirectoryMatch). Commented Mar 28, 2012 at 15:38
  • Sorry, I copy/pasted from the wrong place - I did actually have the line un-commented (fixed in my code now) Commented Mar 29, 2012 at 8:13

1 Answer 1

3
<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>

I found what's wrong. Sorry I didn't pay attention at first:). Probelm is DirectoryMatch is EXACTLY like Directory but it takes regex as argument. So when your document root is /var/www/somedomain.com/httpdocs then uploads directory should be indicated like this

<DirectoryMatch "^/var/www/somedomain.com/httpdocs/uploads/(dir1|dir2)">
      #Somemthibg goes here :-D
 </DirectoryMatch>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.