Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have recently added SSL support for an old Rails 2.3.1 website. I have the following code to redirect from http to https:

Application Controller:

  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

However I got a message from Google:

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

I looked and found that 572 had been excluded from indexing because of the redirect.

How can I then add the correct code for redirecting so I don't lose the indexing?

like image 720
chell Avatar asked Sep 23 '18 10:09

chell


2 Answers

This is what I normally do to migrate my website to https.

Redirect all http traffic to https with 301 redirect in my nginx configure

server {
    listen       80;
    server_name  myawesomewebsite.com;
    return 301   https://myawesomewebsite.com$request_uri;
}

Enable force_ssl in config/application.rb

config.force_ssl = true

Edit: Thank you all for voting my answer. But please also check @agilejoshua 's answer since he put a lot of useful information.

like image 105
H Dox Avatar answered Oct 16 '22 05:10

H Dox


Google guidelines

Google has specific guidelines for moving a site to start using 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

This counts as a site move with URL change

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

Redirecting in Ruby

So in your case you want to ensure that you use 301 redirects. By default redirect_to uses 302 in Ruby.

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:

Updated code for Rails 2.3

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

Alternative code for Rails 3.1+

Use force_ssl as specified in https://edgeguides.rubyonrails.org/configuring.html

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

Indexing issues

But you may still experience temporary issues with indexing. To help google find your new HTTPS pages faster you should create a sitemap with your new HTTPS pages and add it to Google Search Console https://search.google.com/search-console/about.

See https://support.google.com/webmasters/answer/183668 for details of the sitemap formats that Google accepts.

like image 23
agilejoshua Avatar answered Oct 16 '22 05:10

agilejoshua