【问题标题】:Why doesn't this .htaccess code work?为什么这个 .htaccess 代码不起作用?
【发布时间】:2013-12-19 11:49:51
【问题描述】:

已获得此代码以供使用并在不更改的情况下使用它

<FilesMatch "^(?!log_request\.php).*$">
  AuthUserFile /protect/.htpasswd
  AuthName "Tester's test test"
  AuthType Basic
  Require valid-user
</FilesMatch>

ErrorDocument 401 /log_request.php

我不知道 FilesMatch "^(?!log_request.php).*$" 行的功能是什么,但文件应该匹配。当我尝试查看 .htpasswd 文件时,我得到内部服务器错误。

【问题讨论】:

  • 你检查过你的日志吗? /protect/.htpasswd 是否存在(作为绝对路径 - 不是相对路径)?
  • 是的,应该是绝对路径

标签: apache .htaccess


【解决方案1】:

试试这个代码:

<FilesMatch "^(?!.*?log_request\.php).*$">
  AuthUserFile /protect/.htpasswd
  AuthName "Tester's test test"
  AuthType Basic
  Require valid-user
</FilesMatch>

ErrorDocument 401 /log_request.php

(?!...) 是一个否定的前瞻,确保基本身份验证代码应用于除log_request.php 之外的所有文件

选项 2 使用 SetEnvIfNoCase:

SetEnvIfNoCase Request_URI "^/(?!log_request\.php).*" HANDLE401

AuthUserFile /protect/.htpasswd
AuthName "Tester's test test"
AuthType Basic
Require valid-user
Satisfy    any
Order      allow,deny
Allow from  all
Deny from env=HANDLE401

【讨论】:

  • 你做了什么改变,你为什么这么做?我试图真正理解我所看到的。为什么第一行 log_request 的斜线被反转了?
  • 更改在正则表达式中:"^(?!.*?log_request\.php).*$ 确保否定请求 URI 中任何位置的匹配 log_request\.php.*? 是贪婪的模式来匹配它之前的任意文本。
  • 好吧,但我仍然收到内部服务器错误。我试图直接从网站访问 .htaccess 和 .htpasswd ,所以目录是正确的。 .htaccess/.htpasswd 需要特定权限吗?
  • 刚刚测试过,删除 .htaccess 的 FilesMatch 部分会将消息从内部服务错误更改为仅禁止 .htaccess 和 .htpassword,但没有发送身份验证。
  • 500错误的实际原因需要error.log。 FilesMatch 工作正常,我已经测试过了。问题可能出在您的密码文件中。
【解决方案2】:

您可以使用 ErrorDocument 401 /log_request.php 将所有这些请求发送到某个 PHP 脚本。 查看本教程了解更多信息:http://www.askapache.com/htaccess/htaccess-htpasswd-basic-auth.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-05
    • 2012-02-22
    • 2023-03-03
    • 2017-10-02
    • 2016-07-10
    • 2010-12-14
    • 2017-04-09
    相关资源
    最近更新 更多