如何在不丢失 Google 索引的情况下正确地将网站从 http 重定向到 https

How to correctly redirect website from http to https without losing Google Indexing

我最近为旧 Rails 2.3.1 网站添加了 SSL 支持。我有以下代码从 http 重定向到 https:

应用程序控制器:

  before_filter :need_ssl

protected 


  def need_ssl
    if RAILS_ENV=="production"
      redirect_to "https://#{request.host}#{request.request_uri}" unless request.ssl? 
    end
  end

但是我收到了来自 Google 的消息:

Approximately 80% of your HTTP pages that were indexed before migration can no longer be found in either your HTTP or HTTPS site

我查看了一下,发现 572 由于重定向而被排除在索引之外。

然后如何添加正确的重定向代码,以免丢失索引?

这是我通常将我的网站迁移到 https 时所做的。

在我的 nginx 配置

中使用 301 redirect 将所有 http 流量重定向到 https
server {
    listen       80;
    server_name  myawesomewebsite.com;
    return 301   https://myawesomewebsite.com$request_uri;
}

config/application.rb

中启用 force_ssl
config.force_ssl = true

编辑: 感谢大家对我的回答进行投票。但也请检查@agilejoshua 的回答,因为他提供了很多有用的信息。

Rails 3 默认为 302 重定向(临时)。您可以尝试将其更改为 301(永久)重定向,看看 Google 是否更好:

redirect_to("https://#{request.host}#{request.request_uri}", :status => 301) unless request.ssl? 

或等同于:

redirect_to("https://#{request.host}#{request.request_uri}", :status => :moved_permanently) unless request.ssl? 

两个代码片段做完全相同的事情

Google 指南

Google 有关于移动网站以开始使用 SSL 的具体指南

Use server-side 301 redirects

Redirect your users and search engines to the HTTPS page or resource with server-side 301 HTTP redirects.

...

Migrating from HTTP to HTTPS

If you migrate your site from HTTP to HTTPS, Google treats this simply as a site move with URL changes. This can temporarily affect some of your traffic numbers.

https://support.google.com/webmasters/answer/6073543

这算作站点移动 URL 更改

Site move with URL changes

The page URLs change.

For example: The protocol changes — http://www.example.com to https://www.example.com

...

Expect temporary fluctuation in site ranking during the move.

With any significant change to a site, you may experience ranking fluctuations while Google recrawls and reindexes your site. As a general rule, a medium-sized website can take a few weeks for most pages to move in our index; larger sites can take longer. The speed at which Googlebot and our systems discover and process moved URLs largely depends on the number of URLs and your server speed. Submitting a sitemap can help make the discovery process quicker, and it's fine to move your site in sections.

https://support.google.com/webmasters/answer/34437

重定向 Ruby

因此,在您的情况下,您要确保使用 301 重定向。默认情况下 redirect_to 在 Ruby.

中使用 302

v2.3: https://api.rubyonrails.org/v2.3/classes/ActionController/Base.html#M001811

v5.2.1: https://api.rubyonrails.org/v5.2.1/classes/ActionController/Redirecting.html#method-i-redirect_to

The redirection happens as a 302 Found header unless otherwise specified using the :status option:

更新代码 Rails 2.3

redirect_to("https://#{request.host}#{request.request_uri}", :status => 301) unless request.ssl? 

Rails3.1+

的替代代码

按照https://edgeguides.rubyonrails.org/configuring.html

中的指定使用force_ssl

config.force_ssl forces all requests to be served over HTTPS by using the ActionDispatch::SSL middleware, and sets config.action_mailer.default_url_options to be { protocol: 'https' }.

config.force_ssl = true

索引问题

但您可能仍会遇到暂时性的索引问题。为了帮助 google 更快地找到新的 HTTPS 页面,您应该使用新的 HTTPS 页面创建站点地图并将其添加到 Google Search Console https://search.google.com/search-console/about.

有关 Google 接受的站点地图格式的详细信息,请参阅 https://support.google.com/webmasters/answer/183668