【问题标题】:.htaccess password protection doesn't work (redirects to 404).htaccess 密码保护不起作用(重定向到 404)
【发布时间】:2015-09-25 04:23:04
【问题描述】:

在我的公用文件夹中,我有一个想要使用 .htpasswd 保护的日志文件夹。我的应用程序文件夹实际上是在公共内部(该网站是在考虑到这一点的情况下构建的,因此不值得移动应用程序)。当我尝试访问受密码保护的文件夹时,它只是将我重定向到 404。如果我取出 MVC 重写部分,它就可以工作。这是我的目录结构,以防万一。

root/
  public/
    app/
      start.php
      .htaccess
      ...
    logs/
      .htaccess
      .htpasswd
      errorlog.md
    index.php
    .htaccess
  vendor/

这是我认为重要的 /public 中的 .htaccess 部分。

<IfModule mod_rewrite.c>
    Options -Indexes

    RewriteEngine On
    RewriteBase /

    RewriteCond %{HTTP_COOKIE} !cfnw-user [NC]
    RewriteRule ^resources/newspaper http://example.com/error/401 [NC,L]

    # Force to exclude the trailing slash
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.*)/$
    RewriteRule ^(.+)/$ $1 [R=307,L]

    # Allow any files or directories that exist to be displayed directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>

这是 /logs 中的 .htaccess。

AuthType Basic
AuthName "Log files. Sensitive information."
AuthUserFile /home/site/public/logs/.htpasswd
Require valid-user

我想知道如何在不重定向到 404 的情况下使密码保护工作。

【问题讨论】:

    标签: apache .htaccess redirect passwords http-status-code-404


    【解决方案1】:

    /public 文件夹中的 .htaccess 中,您已删除使用 Options -Indexes 的自动索引。这对所有子文件夹都有效,除非您再次更改它。因此,当访问者到达文件夹/logs 时,没有 index.html 或 index.php 文件,并且自动索引已停用,因此 Apache 没有任何显示。 AFAIK 你应该有一个“403 Forbidden”错误,而不是 404 Not found。

    所以把.htaccess文件放到public/logs文件夹中

    Options +Indexes

    并检查您是否已加载/启用 mod_autoindex。

    【讨论】:

    • 不,即使这样,仍然是 404。我去 /logs 或 /logs/errorlog.md 都没关系。还是一样。
    • @Telinc1 那么问题可能出在其他地方。尝试查看错误日志,如果可以,打开 mod_rewrite 日志记录(为此您需要访问服务器配置文件)并查看它的内容。我在这里测试了你的设置和你的规则,它按预期工作,这个答案中提到了两个步骤。
    • 我无权访问服务器配置文件。这可能是一件非常奇怪的事情,只发生在我的服务器上,但不知道为什么。
    猜你喜欢
    • 1970-01-01
    • 2013-01-30
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 2011-12-01
    • 2014-04-20
    • 1970-01-01
    • 2018-02-18
    相关资源
    最近更新 更多