.Htaccess 重写和密码保护文件夹问题
.Htaccess rewrite and password protected folder issue
我整天都在搜索我遇到的问题的解决方案,并找到了许多建议的解决方案,尽管其中 none 可行。我有 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:20am(估计)
我注意到一件事,如果我禁用 url 重写并为文件夹进行身份验证,然后重新启用重写,一切都会按预期工作。它似乎只有在您尚未通过身份验证时才会发生。
您的电子邮件文件夹过滤失败,因为您的条件不匹配。你几乎拥有它。 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]
在尝试更新规则之前还要清除浏览器缓存。
在放弃了一段时间后,我今天回顾了一下,找到了解决方案。贴在下面。 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]
我整天都在搜索我遇到的问题的解决方案,并找到了许多建议的解决方案,尽管其中 none 可行。我有 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:20am(估计)
我注意到一件事,如果我禁用 url 重写并为文件夹进行身份验证,然后重新启用重写,一切都会按预期工作。它似乎只有在您尚未通过身份验证时才会发生。
您的电子邮件文件夹过滤失败,因为您的条件不匹配。你几乎拥有它。 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]
在尝试更新规则之前还要清除浏览器缓存。
在放弃了一段时间后,我今天回顾了一下,找到了解决方案。贴在下面。 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]