2011-12-17 78 views
1

的几个问题有关网页重定向:网页重定向

我想我的主页永远是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] 

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

回答

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访问你的网站,所以没有必要设置在谷歌网站管理员工具的首选域。

+0

感谢您的回复。所以如果我正确地关注你,你所说的是,如果我使用你提供的代码并拿出我在那里的代码,它会照顾所有必要的重定向,包括http://www.mydomain .com/index.html,http://mydomain.com/index.html&http://mydomain.com/,使他们都去http://www.mydomain.com/ ?? – Andy 2011-12-18 05:56:26

+0

如果您使用上面的代码,其中包含您的index.html规则,它应该按照您的要求工作 – 2011-12-18 17:56:58