【问题标题】:.htaccess in subdirectory always refers to root子目录中的 .htaccess 总是指根目录
【发布时间】:2014-12-21 21:03:21
【问题描述】:

我的 .htaccess 遇到了一个非常奇怪的行为。每当我尝试访问由 mod_rewrite 重写的链接时,我的 .htaccess 引用的是根目录,而不是我正在使用的子目录。我的文件夹结构如下所示:
htdocs/ blog6/ .htaccess
我的 .htaccess 看起来像这样:

Options +FollowSymLinks -MultiViews

ErrorDocument 401 /core/error/401.php
ErrorDocument 403 /core/error/403.php
ErrorDocument 404 /core/error/404.php
ErrorDocument 500 /core/error/500.html

RewriteEngine on
RewriteBase /blog6/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /index.php?page=$1 [L]

但是每当我尝试通过重写的 url 访问文件时,我都会收到 404 错误,并且日志显示:

C:/xampp/htdocs/index.php' not found or unable to stat

所以看起来我的 .htaccess 想要访问 htdocs 中的文件而不是使用子目录。当我在我的 rewriteRule 中写 /blog6 时,一切正常,但为什么 RewriteBase 不能正常工作?如果它很重要,我在我的 html 中使用 <base>

【问题讨论】:

  • 您在浏览器中打开的哪个 URL 会导致此 404?我相信这个 .htaccess 在/blog6/ 目录中?
  • @anubhava 是的,.htaccess 在/blog6/ 中。我打开http://localhost/blog6/login,它应该是http://localhost/blog6/index.php?page=login 的重写形式。如果我在 rewriteRule 前面放一个blog6/,它看起来像这样:RewriteRule ^([^/]*)$ /blog6/index.php?page=$1 [L] 并且可以工作。这就是让我感到困惑的地方,因为 rewriteBase 没有影响......

标签: apache .htaccess mod-rewrite redirect rewrite


【解决方案1】:

RewriteBase 仅在您在 RewriteRule 的目标中提供 相对 URL 时才有效,因此请将您的代码更改为:

Options +FollowSymLinks -MultiViews

ErrorDocument 401 /core/error/401.php
ErrorDocument 403 /core/error/403.php
ErrorDocument 404 /core/error/404.php
ErrorDocument 500 /core/error/500.html

RewriteEngine on
RewriteBase /blog6/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?page=$1 [L,QSA]

【讨论】:

    猜你喜欢
    • 2013-10-02
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 2013-09-12
    • 1970-01-01
    • 2012-01-26
    • 2011-08-24
    • 2014-10-24
    相关资源
    最近更新 更多