【问题标题】:.htaccess RewriteRule causing "file not found".htaccess RewriteRule 导致“找不到文件”
【发布时间】:2015-04-07 18:58:13
【问题描述】:

我的网站托管在一个子文件夹中:site.com/foo/

foo/ 文件夹的根目录中,我有以下.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule (.*) webroot/$1 [L]
</IfModule>

它的作用是将所有页面请求重定向到位于foo/ 文件夹中的webroot/ 子文件夹。 在那个 webroot/ 子文件夹中,我有以下 .htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php/$1 [L]

目标是所有都应该重定向到index.php。需要明确的是,文件结构是:

www/
  foo/
      .htaccess
      webroot/
         .htaccess
         index.php

当我尝试访问site.com/foo/ 时,我按预期得到index.php,但是当我尝试诸如site.com/foo/bar/bar/bar 之类的随机事物时,我得到“找不到文件”(仅此而已)但我仍然想要index.php .

它仅在访问site.com/foo/ 时起作用的原因与第二个.htaccess 无关,它只是webroot/ 默认为其包含的index.php 提供服务。

注意:它在localhost 中工作,localhost 和我现在使用的服务器之间的主要区别可能是该站点在ROOT 中为localhost 但现在在一个子文件夹中 (@987654343 @)。

注意 2:如果我从 index.php/$1 中删除 /$1,它会起作用,它将所有重定向到 index.php,但没有我需要的参数(在上面的示例中为 bar/bar/bar。)

如何让所有请求重定向到带有参数的 index.php?

【问题讨论】:

    标签: php apache .htaccess mod-rewrite redirect


    【解决方案1】:

    尝试在两个 .htaccess 中添加RewriteBase

    /foo/.htaccess:

    RewriteEngine On
    RewriteBase /foo/
    
    RewriteRule (.*) webroot/$1 [L]
    

    /foo/webroot/.htaccess:

    RewriteEngine On
    RewriteBase /foo/webroot/
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule (.*) index.php/$1 [L]
    

    【讨论】:

    • 浏览器中的完整网址是什么?您遇到了什么错误?
    • 网址是“site.com/foo/bar/”,我得到的是“找不到文件”。它在不通过 index.php 的情况下查找该 url
    • 测试/foo/.htaccess中的更改规则到RewriteRule (.*) webroot/$1 [L,R]然后加载site.com/foo/bar
    • 这会重定向到 (secret)/www/foo/webroot/bar ... 看起来确实没有考虑 index.php
    • 为什么/www//foo/ 之前?我以为你的网络根是/www/
    【解决方案2】:

    QSA 标志附加查询字符串-

    重写规则 (.*) index.php?data=$1 [QSA]

    【讨论】:

      猜你喜欢
      • 2012-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-10
      • 2019-07-13
      • 2011-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多