主页重定向

时间:2011-12-17 17:44:16

标签: .htaccess redirect

关于主页重定向的一些问题:

我希望我的主页始终为http://www.mydomain.com/

我已经在htaccess中使用了以下代码一段时间,以确保将http://www.mydomain.com/index.html重定向到http://www.mydomain.com/(至少我认为这就是它的用途)。

RewriteEngine on 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/ 
RewriteRule ^index\.html$ http://www.mydomain.com/ [R=301,L] 

我是否还需要添加如下所示的单独代码才能将http://mydomain.com/重定向到http://www.mydomain.com/?或者上面的代码是否同时完成? (因为两个重定向似乎已经在工作,我只是不确定为什么它已经重定向到http://mydomain.com/

RewriteEngine on
RewriteCond % ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]

此外,如果我使用这些301重定向,是否有必要将Google网站管理员工具中的首选域设置为www.mydomain.com?

1 个答案:

答案 0 :(得分:0)

您当前的规则不会同时处理mydomain.com/about.html不会重定向到www.mydomain.com/about.html(假设about.html存在) 如果您想确保用户只能通过www.mydomain.com访问您的网站,请将以下规则添加到.htaccess文件中。

RewriteEngine on
RewriteBase /

#if the domain is not www.mydomain.com
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$ [NC]
#redirect to www.mydomain.com
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]

#leave this rule in place, but after the one above to handle the home page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/ 
RewriteRule ^index\.html$ http://www.mydomain.com/ [R=301,L] 

有了这条规则,您只能通过www.mydomain.com访问您的网站,因此无需在Google网站管理员工具中设置首选域。

相关问题