.htaccess密码保护目录但允许图像文件类型

时间:2022-09-16 23:01:40

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.

我需要使用.htaccess密码保护目录,我已成功完成。但是网站的前端被编程为链接到这个受密码保护的目录(不是我)中的图像,但是当网页试图访问这些图像时,它会提示用户登录。

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

是否可以密码保护该目录,但允许任何图像文件类型的访问,如* .jpg和* .gif?

My current .htaccess code is this:

我目前的.htaccess代码是这样的:

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

Thanks for any help!

谢谢你的帮助!

4 个解决方案

#1


15  

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

编辑以融入Shef的改进

#2


1  

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

您可以检查配置的所有不同选项.htaccess在以下站点中为您提供:

Stupid htaccess Tricks

愚蠢的htaccess技巧

#3


0  

Did you try put it inside Filematch?

你试过把它放在Filematch中吗?

<FilesMatch "^.*(png|jpe?g|gif)$">
AuthName "Secure Area"
AuthUserFile "/home/siteuser/.htpasswds/public_html/admin/passwd"
AuthType Basic
require valid-user
</FilesMatch>

#4


-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.
  • 在链接图像的网站根目录上的.htaccess中,为所需的图像类型添加“重写”规则。此规则应将调用重定向到PHP处理程序脚本。
  • 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.
  • 该脚本应评估所请求的路径,从文件系统加载文件,扣除其标头并使用header()将其发送到客户端,然后是映像文件的内容echo file_get_contents()应该这样做。

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.

PHP不受.htaccess的影响,因此它应该能够读取您需要的文件并将其代理给最终用户。

#1


15  

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

编辑以融入Shef的改进

#2


1  

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

您可以检查配置的所有不同选项.htaccess在以下站点中为您提供:

Stupid htaccess Tricks

愚蠢的htaccess技巧

#3


0  

Did you try put it inside Filematch?

你试过把它放在Filematch中吗?

<FilesMatch "^.*(png|jpe?g|gif)$">
AuthName "Secure Area"
AuthUserFile "/home/siteuser/.htpasswds/public_html/admin/passwd"
AuthType Basic
require valid-user
</FilesMatch>

#4


-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.
  • 在链接图像的网站根目录上的.htaccess中,为所需的图像类型添加“重写”规则。此规则应将调用重定向到PHP处理程序脚本。
  • 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.
  • 该脚本应评估所请求的路径,从文件系统加载文件,扣除其标头并使用header()将其发送到客户端,然后是映像文件的内容echo file_get_contents()应该这样做。

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.

PHP不受.htaccess的影响,因此它应该能够读取您需要的文件并将其代理给最终用户。