.htaccess将root重定向到index.php

时间:2012-04-16 14:14:17

标签: apache .htaccess mod-rewrite root

我需要从http://example.com/重定向到http://example.com/index.php

4 个答案:

答案 0 :(得分:17)

使用此:

DirectoryIndex index.php

答案 1 :(得分:4)

试试这个:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} ^$
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^$ http://example.com/index.php [L,R=301]

答案 2 :(得分:0)

仅添加我自己的解决方案,因为the answer of Michael对我不起作用,所以我做了类似的事情:

RewriteEngine on

# These two lines redirect non-www to www.
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) https://www.example.com$1 [R=301,L]

# These two lines redirect the root to index.html.
RewriteRule ^$ /index.html [R=301,L]
RewriteRule ^/$ /index.html [R=301,L]

这还将保留所有可能的现有查询字符串参数。

答案 3 :(得分:0)

加上上面的 @Uwe Keim's answer,我在 Apache 虚拟主机上设置了非 www 到 www。

对我有用的 .htaccess 是:

RewriteEngine on

# Two lines to redirect from the root web directory to myfile.html.
RewriteRule ^$ /index.html [R=301,L]
RewriteRule ^/$ /index.html [R=301,L]
相关问题