【问题标题】:.htaccess to redirect everything to index.php, but keep a copy of the website in a subdirectory.htaccess 将所有内容重定向到 index.php,但在子目录中保留网站的副本
【发布时间】:2015-07-31 02:54:04
【问题描述】:

我有一个简单的 .htaccess 文件。我希望它将不是文件或目录的所有内容重定向到 index.php。我在子文件夹may9-new 中保留了该网站的另一个副本(将相同的 .htaccess 文件复制到该目录中)。但问题是,我可以成功访问www.example.com/may9-new,但如果我转到www.example.com/may9-new/anything,我似乎在根文件夹中看到了该站点的版本。这是 .htaccess 文件:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/may9-new
RewriteRule ^([^?]*)$ /index.php?path=$1 [NC,L,QSA] 
</IfModule>

我对 .htaccess 几乎一无所知,所以请耐心等待。我了解到RewriteCondRewriteRule 应该发生的条件。所以我试着写了一个条件,路径不能以/may9-new开头。这个RewriteCond 仅适用于www.example.com/may9-new,如果添加了更多的斜线则无效。

有人可以帮我找出正确的规则吗?

谢谢!

【问题讨论】:

    标签: php apache .htaccess mod-rewrite redirect


    【解决方案1】:

    不要在目标中使用绝对路径/。这是您可以保存在根目录和子目录中的 .htaccess:

    DirectoryIndex index.php
    <IfModule mod_rewrite.c>
    RewriteEngine on
    
    RewriteRule ^index\.php$ - [L,NC]
    
    # If the request is not for a valid directory
    RewriteCond %{REQUEST_FILENAME} !-d
    # If the request is not for a valid file
    RewriteCond %{REQUEST_FILENAME} !-f
    # route to index.php in current directory
    RewriteRule ^(.+)$ index.php?path=$1 [L,QSA] 
    </IfModule>
    

    【讨论】:

    • 我认为您正在做一些事情,但是我收到了一个与此 .htaccess 文件有关的 Internet 服务器错误。 内部服务器错误 服务器遇到内部错误或配置错误,无法完成您的请求。请联系服务器管理员并告知他们错误发生的时间,以及您在此错误之前执行的操作。服务器错误日志中可能会提供有关此错误的更多信息。
    • may9-new 没有index.php 吗?您可以检查 Apache error.log 以查看 500 错误的错误是什么
    • 它肯定有一个并且它有效。但是其他页面不起作用。让我弄清楚如何检查错误日志。
    • 上面写着[Tue May 19 03:40:38 2015] [10528350] [core:error] [client 172.56.28.46:45305] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace. 奇怪的是,这个带有 .htaccess 文件的网站在 Dreamhost 托管的不同域上工作(使用 godaddy 来做这个)。
    • 你试过我更新的答案了吗? Request exceeded the limit of 10 只能在当前目录中没有index.php 的情况下存在。
    【解决方案2】:

    也许您需要将您的配置基于您的主机系统。尝试检查这些:

    RewriteCond %{REQUEST_URI} !^/may9-new(.*)
    RewriteRule ^(.+)$ /index.php?path=$1 [NC,QSA]
    

    您也可以就这个问题联系您的托管服务提供商:

    奇怪的是,这个带有 .htaccess 文件的网站在 由...托管的不同域

    您的 "may9-new" 子文件夹中是否也有相同的 .htaccess 文件?

    我在子文件夹 may9-new 中保留了该网站的另一个副本 (将相同的 .htaccess 文件复制到该目录中)。

    如果你也有,那么可能有冲突,尝试修改你的子文件夹的.htaccess文件:

    RewriteRule ^(.+)$ /may9-new/index.php?path=$1 [NC,QSA]
    

    【讨论】:

    • 原来 may9-new 中的 .htaccess 文件有错误的文件名,但我更新了您的编辑并改回了名称,现在我得到了某种无限重定向循环......另一个答案中提到的相同的互联网服务器错误。
    • @NickManning 请记住始终使用!-f!-d 检查文件是否不存在。在您的子文件夹中试试这个:RewriteRule ^/may9-new/(.+)$ /may9-new/index.php?path=$1 [NC,QSA]
    • 有了这个规则,我得到了同样的原始问题......它似乎在根文件夹中显示了站点的版本。当我切换回来时,这是一个无限重定向。
    • @NickManning 您需要在根目录中添加此指令:RewriteCond %{REQUEST_URI} !^/?may9-new(.*) 您的系统上是否有任何 .htaccess 文件?
    • 该条件已添加。我正在使用godaddy ......目前根目录中只有一个.htaccess 文件,正是你告诉我的。它可以正常工作,就像我的原始文件一样,但我在子目录中仍然遇到同样的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    • 2012-10-16
    • 1970-01-01
    • 2019-11-29
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多