【问题标题】:.Htaccess rewrite and password protected folder issue.Htaccess 重写和密码保护文件夹问题
【发布时间】:2015-02-19 23:37:33
【问题描述】:

我整天都在寻找解决我遇到的问题的方法,并找到了许多建议的解决方案,尽管它们都没有奏效。我设置了 url 重写,除了一个文件夹外,一切正常。如果存在文件夹或文件,请不要重写,否则将其发送到 index.php 中进行处理。出现问题的一个子文件夹受密码保护。受密码保护的文件夹被重新写入 index.php,就好像子文件夹无效一样。其他子文件夹工作正常,并且其中没有 .htaccess。文件夹结构和 .htaccess 下面。

包含相关文件的文件夹结构

  • public_html(根)
    • 字体
    • 图片
    • js
    • CSS
    • 电子邮件
      • .htaccess
      • index.php
    • .htaccess
    • index.php

public_html/.htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php [L]

public_html/email/.htaccess

AuthType Basic
AuthName "email"
AuthUserFile "/home/site/.htpasswds/public_html/email/passwd"
require valid-user

我尝试关闭电子邮件文件夹内的重写以防止在此解决方案中传播的重写表单没有明显效果。

## turn off rewrite engine
RewriteEngine off

AuthType Basic
AuthName "email"
AuthUserFile "/home/site/.htpasswds/public_html/email/passwd"
require valid-user

我还尝试过滤掉根 .htaccess 中的文件夹,这似乎也被忽略了,没有明显区别。

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^email
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

对此的任何帮助将不胜感激!

2015 年 2 月 23 日更新:上午 11:20(美国东部时间)

我注意到的一件事是,如果我禁用 url 重写并对文件夹进行身份验证,然后重新启用重写,一切都会按预期工作。它似乎只有在您尚未通过身份验证时才会发生。

【问题讨论】:

    标签: php apache .htaccess url-rewriting


    【解决方案1】:

    在放弃了一段时间后,我今天回顾了这个问题并找到了解决方案。贴在下面。 401 错误被 apache 解释为 404 错误并传递给重写。解决方法是在开启 RewriteEngine 之前添加 ErrorDocument 401 default。告诉它显式使用默认错误消息似乎是多余的,但它确实解决了错误。

    ErrorDocument 401 default
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} !^email [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    

    【讨论】:

      【解决方案2】:

      您的过滤掉电子邮件文件夹不起作用,因为您的条件不匹配。你几乎拥有它。 REQUEST_URI 也与前面的 / 匹配,所以因为您在 email 之前没有它,所以它不匹配。在条件中使用 REQUEST_URI 时必须包含它。这是一个真正的文件夹,所以你不应该需要它,但可以这样尝试。

      RewriteEngine On
      RewriteBase /
      RewriteCond %{REQUEST_URI} !^/email [NC]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule . /index.php [L]
      

      在尝试更新规则之前,还要清除浏览器缓存。

      【讨论】:

      • 不幸的是,这显然没有效果。它仍然被 ./index.php 拾取
      • 该条件确实有效,因此您的配置可能有问题。你确定你清除了浏览器缓存?在其他浏览器中尝试。
      • 在不同的浏览器和不同的计算机上尝试过,结果相同。如果我已经对文件夹进行了身份验证,它会按预期工作,但不会。
      猜你喜欢
      • 2014-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-03
      • 1970-01-01
      相关资源
      最近更新 更多