【问题标题】:homepage redirects主页重定向
【发布时间】:2011-12-17 17:44:15
【问题描述】:

关于主页重定向的几个问题:

我希望我的主页始终是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 重定向,是否有必要在谷歌网站管理员工具中将首选域设置为 www.mydomain.com?

【问题讨论】:

    标签: .htaccess redirect


    【解决方案1】:

    您当前的规则不会同时处理这两种情况,例如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访问你的网站,所以不需要在谷歌站长工具中设置首选域。

    【讨论】:

    • 感谢您的回复。因此,如果我正确地关注您,您的意思是,如果我使用您提供的代码并取出其中的代码,它将处理所有必要的重定向,包括mydomain.com/index.htmlmydomain.com/index.html &mydomain.com,让他们都去mydomain.com ??
    • 如果您使用上面的代码,其中包括您对 index.html 的规则,它应该可以按照您的要求工作
    猜你喜欢
    • 1970-01-01
    • 2013-03-14
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-17
    相关资源
    最近更新 更多