14

I need to password protect a directory with .htaccess, which I have successfully done. But the front end of the website was programmed to link to images within this password protected directory (not by me), but when a webpage tries to access those images it prompts the user to login.

Is it possible to password protect that directory, but allow any access to any image file type like *.jpg and *.gif?

My current .htaccess code is this:

AuthName "Secure Area"
AuthUserFile "/home/siteuser/.htpasswds/public_html/admin/passwd"
AuthType Basic
require valid-user

Thanks for any help!

4 Answers 4

19
AuthName "Secure Area"
AuthUserFile "/home/siteuser/.htpasswds/public_html/admin/passwd"
AuthType Basic
require valid-user
<FilesMatch "\.(png|jpe?g|gif)$">
  Satisfy Any
  Allow from all
</FilesMatch>

Edit to incorporate Shef's improvement

1
  • 1
    +1. Short & Sweet! Though the regex should be changed to match only from the end of the string FilesMatch "\.(png|jpe?g|gif)$".
    – Shef
    Commented Jul 28, 2011 at 18:39
1

You could check all the different options of configuration .htaccess gives you in the following site:

Stupid htaccess Tricks

0

Did you try put it inside Filematch?

<FilesMatch "^.*(png|jpe?g|gif)$">
AuthName "Secure Area"
AuthUserFile "/home/siteuser/.htpasswds/public_html/admin/passwd"
AuthType Basic
require valid-user
</FilesMatch>
1
  • 1
    this won't work. it secures images but NOT other documents. Opposite of what OP want
    – genesis
    Commented Jul 28, 2011 at 18:11
-1

What you could try is to write an image display proxy:

  • Keep the directory like you have it now, with password protection.
  • On the .htaccess on the root of the website where the images are linked, add a Rewrite rule for those image types you want. This rule should redirect the call to a PHP handler script.
  • That script should evaluate the path that was being requested, load the file from the filesystem, deduct its header and send that to the client using header(), followed by the image file's content echo file_get_contents()should do.

PHP is not affected by the .htaccess so it should be able to read the file you need and proxy it to the end user.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.