php - .htaccess 身份验证 w/friendly url

标签 php .htaccess authentication friendly-url

我正在尝试设置我的 .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 部分似乎也不起作用..

有人可以帮我吗?

谢谢!

最佳答案

您需要解决两件事。

  1. 你的 <FilesMatch>容器什么都不做。你已经有一个 Satisfy Any .您应该将这些扩展添加到单独的 SetEnvIfNoCase

  2. 当您使用重写规则重写 /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 than index.php, then from the intermediate script, call index.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/

相关文章:

wordpress - 将带有片段标识符的 Ecwid URL 重定向到新的 Woocommerce URL

php - 为什么这个 htaccess 文件在 wamp 服务器中不起作用

javascript - bxslider 自定义下一张幻灯片按钮

php - 为什么我的 SQL 查询没有将数据放入数据库?

php - 如何通过 SOAP api 更新团体价格

apache - 如何在 Nuxt 路由器中手动生成带有 .htaccess 404 页面回退的页面

java - Android 中的基本身份验证登录

java - 目前实现REST/J2EE/Database + custom auth的工具有什么好的组合

api - 如何通过身份验证访问 OData URL

php - 打开新页面并在同一 href 链接中执行 javascript