【问题标题】:.htaccess rewrite but exclude password-protected directory results in 404 error.htaccess 重写但排除受密码保护的目录会导致 404 错误
【发布时间】:2013-07-22 23:28:43
【问题描述】:

我有一个带有 .htaccess 的漂亮 URL 设置,它将不存在的 URL(不是严格的 404 错误)重写为提供内容或 404 消息的 PHP 脚本。我已经从这次重写中排除了现有文件和目录,这样 CSS、图像等就不会受到影响。

但是,有一个受 .htpasswd 密码保护的目录也应该绕过重写规则,但在为其启用 .htpasswd 时不会。

父目录.htaccess:

RewriteEngine On

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /callbacks.php [L]

有一个带有以下 .htaccess 的子目录:

RewriteEngine on

Options All -Indexes

AuthName "Backstage Access" 
AuthType Basic 
AuthUserFile /path/to/.htpasswd
AuthGroupFile /dev/null 
require valid-user

如果我删除 require valid-user 行,父目录的重写规则会根据需要忽略子目录,我可以从浏览器访问它。但是我开启授权的时候,父目录的.htaccess将子目录重写为/callbacks.php,导致自定义404错误。

关于如何解决这个问题的任何想法?显然,我无法消除对密码保护的需求。我尝试删除父重写规则,而是简单地使用/callbacks.php 作为 404 错误文档,但随后 POST 和 GET 数据未传输。

【问题讨论】:

    标签: apache .htaccess mod-rewrite .htpasswd


    【解决方案1】:

    我不确定它是否仍然存在,但我遇到了同样的问题并找到了解决方法 - 在您的 htaccess 中添加以下行:

    ErrorDocument 401 /somepage.html
    ErrorDocument 403 /somepage.html
    

    这应该足以绕过重写器

    【讨论】:

      猜你喜欢
      • 2011-11-07
      • 1970-01-01
      • 1970-01-01
      • 2013-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多