htaccess 将 index.php 重定向到 root(包括子域)

问题描述:

我正在尝试将 index.php 文件重定向到根/,我四处搜索并找到了几个类似于以下内容的代码片段:

I'm trying to redirect index.php files to the root /, I've searched around and found several snippets of code similar to:

RewriteCond %{THE_REQUEST} ^.*/index.php 
RewriteRule ^(.*)index.php$ http://www.domain.com/$1 [R=301,L] 

但是这不适用于子域,示例如下:

This however won't work for subdomains, example below:

  1. 我有一个子域:subdomain.domain.com
  2. 用户访问了 http://subdomain.domain.com/index.php
  3. 使用上面的 htaccess,它们被重定向到 http://www.domain.com/ 而不是 http://subdomain.domain.com/

有谁知道如何调整上面的 htaccess 以便它考虑子域并将它们重定向到适当的位置?

Does anyone know how to adjust the htaccess above so it will take into account the subdomain and redirect them to the appropriate location?

例如如果用户访问 http://subdomain.domain.com/index.php,他们将转到 http://subdomain.domain.com/

e.g. if a user visits http://subdomain.domain.com/index.php they will go to http://subdomain.domain.com/

奖励积分:

是否有一个 htaccess 可以将此规则应用于所有文件夹?

Is there a htaccess that can just apply this rule to all folders?

因此,对于任何带有 index.php 的文件夹,它们只会被重定向到它的根目录,例如http://subdomain.domain.com/folder1/folder2/index.php 会自动转到 http://subdomain.domain.com/folder1/folder2/

So for any folder with an index.php they will just be redirected to it's root e.g. http://subdomain.domain.com/folder1/folder2/index.php would automatically go to http://subdomain.domain.com/folder1/folder2/

这样做:

RewriteCond %{THE_REQUEST} ^.*/index\.php 
RewriteRule ^(.*)index.php$ /$1 [R=301,L]