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

时间:2012-02-20 23:19:56

标签: apache mod-rewrite

我正在尝试将index.php文件重定向到root /,我搜索了一下,发现了几段代码类似于:

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

然而,这不适用于子域名,例如:

  1. 我有一个子域名:subdomain.domain.com
  2. 用户访问http://subdomain.domain.com/index.php
  3. 使用上面的htaccess,它们被重定向到http://www.domain.com/而不是http://subdomain.domain.com/
  4. 有没有人知道如何调整上面的htaccess以便它会考虑子域并将它们重定向到适当的位置?

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

    奖励积分:

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

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

3 个答案:

答案 0 :(得分:74)

这样做:

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

答案 1 :(得分:11)

我在@ThinkingMonkey's answer

上添加了以下代码
RewriteRule ^(.*)index\.(php|html?)$ /$1 [R=301,NC,L]

它将以 index.php index.htm index.html 结尾的URI重定向到 / 。也适用于子目录。

另外,请注意NC标志。它使它不区分大小写。所以即使URI是大写的,例如 INDEX.PHP ,它也能正常工作。

答案 2 :(得分:-3)

可以使用这个

RewriteRule ^index\.php/(.*)$ http://www.example.com/$1 [R=301,L]
相关问题