我正在尝试设置我的 .htaccess 文件以在我们的开发网站上启用基本身份验证。
我希望身份验证适用于我的所有站点,但只针对某些 URL。我们在我们的网站上使用友好的 URL..
我还想绕过某些文件类型(如 png、css 等)的身份验证...
到目前为止,我所看到的是这样的:
SetEnvIfNoCase Request_URI ^/upload/ NO_AUTH
AuthName "Restricted Area"
AuthType Basic
AuthUserFile /path/to/.htpasswd
Require valid-user
Satisfy any
Order deny,allow
Deny from all
Allow from env=NO_AUTH
<FilesMatch "\.(gif|jpe?g|png|css|js|swf)$">
Satisfy Any
</FilesMatch>
身份验证工作正常,但 SetEnvIfNoCase
似乎不起作用,因为在转到友好 URL“/upload/”时仍然要求我进行身份验证。
FilesMatch
部分似乎也不起作用..
有人可以帮我吗?
谢谢!
最佳答案
您需要解决两件事。
你的
<FilesMatch>
容器什么都不做。你已经有一个Satisfy Any
.您应该将这些扩展添加到单独的SetEnvIfNoCase
当您使用重写规则重写
/upload/
时,您需要包括“友好”URI 以及重写的 URI,因为验证模块在每一步都被应用(显然,因为允许通过重写/别名绕过身份验证真的很糟糕)。这意味着,例如,如果您有此规则:RewriteRule ^upload/(.*)$ /upload_script.php?file=$1 [L]
那么您需要为两者设置NO_AUTH 行/upload/
和 /upload_script.php
:
SetEnvIfNoCase Request_URI ^/upload/ NO_AUTH
SetEnvIfNoCase Request_URI ^/upload_script.php NO_AUTH
然后到地址#1:
SetEnvIfNoCase Request_URI \.(gif|jpe?g|png|css|js|swf)$ NO_AUTH
所以建议的一个例子:
something that you can try is to add a rule (similar to the above
RewriteRule
example), to route the/upload/
to an intermediate script other thanindex.php
, then from the intermediate script, callindex.php
.
所以使用上面的例子/upload_script.php
您可以在您的任何规则之前添加:
RewriteRule ^upload/ /upload_script.php [L]
所以它看起来像:
RewriteEngine On
RewriteRule ^upload/ /upload_script.php [L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
然后,您创建一个名为 upload_script.php
的脚本这将有类似的东西:
<?php
include_once "index.php";
?>
您可能想要转储 $_SERVER['']
array 首先查看是否所有内容都设置为 Zend 的 index.php
期待看到。
关于php - .htaccess 身份验证 w/friendly url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25122685/