如何正确地将网站从http重定向到https而不会丢失Google Indexing

che*_*ell 8 https ruby-on-rails http-redirect

我最近为旧的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
Run Code Online (Sandbox Code Playgroud)

但是我收到了谷歌的消息:

Approximately 80% of your HTTP pages that were indexed before migration can no longer be found in either your HTTP or HTTPS site
Run Code Online (Sandbox Code Playgroud)

我查看并发现572因为重定向而被排除在索引之外.

我怎样才能添加正确的重定向代码,这样我就不会丢失索引?

H D*_*Dox 6

这是我通常将我的网站迁移到的地方https.

重定向所有http流量,https301 redirect我的nginx配置

server {
    listen       80;
    server_name  myawesomewebsite.com;
    return 301   https://myawesomewebsite.com$request_uri;
}
Run Code Online (Sandbox Code Playgroud)

启用force_sslconfig/application.rb

config.force_ssl = true
Run Code Online (Sandbox Code Playgroud)

编辑:谢谢大家投票给我答案.但请同时查看@agilejoshua的回答,因为他提供了大量有用的信息.


agi*_*hua 3

谷歌指南

\n

Google 针对将网站迁移为开始使用 SSL 制定了具体指南

\n
\n

使用服务器端 301 重定向

\n

使用服务器端 301 HTTP 重定向将您的用户和搜索引擎重定向到 HTTPS 页面或资源。

\n

...

\n

从 HTTP 迁移到 HTTPS

\n

如果您将网站从 HTTP 迁移到 HTTPS,Google 会将其视为随 URL 更改而发生的网站移动。这可能会暂时影响您的部分流量。

\n
\n

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

\n

这算作随着 URL 更改而发生的站点移动

\n
\n

站点随 URL 更改而移动

\n

页面 URL 发生变化。

\n

例如:协议将 \xe2\x80\x94 http://www.example.com更改为https://www.example.com

\n

...

\n

预计迁移期间网站排名会出现暂时波动。

\n

如果网站发生任何重大更改,当 Google 重新抓取您的网站并重新编制索引时,您可能会遇到排名波动。一般来说,中型网站的大多数页面可能需要几周的时间才能进入我们的索引;较大的站点可能需要更长的时间。Googlebot 和我们的系统发现和处理移动网址的速度很大程度上取决于网址数量和服务器速度。提交站点地图可以帮助加快发现过程,并且可以分段移动站点。

\n
\n

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

\n

Ruby 中的重定向

\n

因此,在您的情况下,您需要确保使用 301 重定向。默认重定向到在 Ruby 中使用 302。

\n

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

\n

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

\n
\n

除非使用 :status 选项另有指定,否则重定向以 302 Found 标头形式发生:

\n
\n

更新了 Rails 2.3 的代码

\n
redirect_to("https://#{request.host}#{request.request_uri}", :status => 301) unless request.ssl? \n
Run Code Online (Sandbox Code Playgroud)\n

Rails 3.1+ 的替代代码

\n

使用https://edgeguides.rubyonrails.org/configuring.html中指定的force_ssl

\n
\n

配置.force_ssl使用 ActionDispatch::SSL 中间件强制所有请求通过 HTTPS 提供服务,并将 config.action_mailer.default_url_options 设置为 { protocol: \'https\' }。

\n
\n
config.force_ssl = true\n
Run Code Online (Sandbox Code Playgroud)\n

索引问题

\n

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

\n

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

\n