.htaccess允许图像在一个子文件夹中

时间:2016-07-27 21:42:31

标签: .htaccess

我有一个网站和.htaccess文件,禁用对所有图像和pdf文件的访问。 我添加了一个新的子文件夹,我需要允许外部访问此子文件夹中的图像和PDF。 我该怎么做?

RewriteEngine on
AuthName "myname"
AuthUserFile "/home/virtual/website/.htpasswds/public_html/mywebsite/passwd"
<files ~ "^(admin(.*)\.(php|js))$">
require valid-user 
</files>

RewriteCond %{HTTP_REFERER} !^http://website.com/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http://website.com$      [NC]
RewriteCond %{HTTP_REFERER} !^http://www.website.com/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http://www.website.com$      [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp|pdf)$ - [F,NC]

2 个答案:

答案 0 :(得分:1)

您应该能够添加条件以排除子文件夹

RewriteCond %{HTTP_REFERER} !^http://website.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://website.com$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.website.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.website.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subfolder [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp|pdf)$ - [F,NC]

答案 1 :(得分:1)

只需添加另一个RewriteCond即可检查子文件夹:

RewriteCond ${REQUEST_URI) !^/sub-folder/ [NC]
RewriteCond %{HTTP_REFERER} !^http://website.com/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http://website.com$      [NC]
RewriteCond %{HTTP_REFERER} !^http://www.website.com/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http://www.website.com$      [NC]
RewriteRule \.(jpe?g|gif|png|bmp|pdf)$ - [F,NC]