Drupal 8 - Transition from HTTP to HTTPS for better Google ranking

By xngo on June 16, 2019

I have read for a long time that Google will rank your website higher if it is using HTTPS but I haven't make the move until now. Today, I took the plunge and it is not straight forward. In this tutorial, I will show you what I have done to complete the transition. It might be helpful for other people to know what to consider before taking the plunge.

Enable SSL for your domain

My website is hosted on Bluehost.com. They offer free SSL(AutoSSL). So, I followed their instructions from here but didn't change the .htaccess file. It worked without it and I don't want to mess around with .htaccess file.

Regenerate sitemap

Enabling SSL is not the end of the move. You need to also regenerate your sitemap.xml. My old sitemap.xml listed all URLs as HTTP. You also have to re-submit your sitemap to search engines.

Fix internal links

Until now, I thought everything was fine. I continued to test by browsing through some articles and noticed that the SSL lock icon on the left side the URL is not green. It has a warning saying that parts of the page are not secure.

I opened the article in edit mode and noticed that for all my images, I put the full URL path. Now, the tedious part started. I had to fix all http for all my nodes. In this process, I have learned my lesson: All internal links should be using relative URLs.

You can use the following SQL query to update your table but be careful, backup your databse first.

-- Template SQL
UPDATE your_table
SET your_field = REPLACE(your_field, 'http://www.domain.com', 'https://www.domain.com')
WHERE your_field LIKE '%http://www.domain.com%';
 
-- I use this SQL to test out node 261 and it worked.
UPDATE `PREFIX_node__field_pg_body`
SET `field_pg_body_value` = REPLACE(`field_pg_body_value`, 'http://openwritings.net/sites', '/sites')
WHERE `field_pg_body_value` LIKE "%http://openwritings.net/sites/default/%"
    AND `entity_id` = 261

After you ran the SQL, make sure to:

  • clear all your cache. Otherwise, old content of the nodes will be shown when you are in edit mode.
  • Re-index your Search pages.

Force all links to HTTPS

My website is hosted in a subdirectory. I don't have access to Apache configuration file to set the virtual host properly. The best that I can do is to write rules for mod_rewrite to redirect all links to use HTTPS. The only caveat is http://openwritings.net will not be redirected but any other links will. So, my Apache rewrite rules are:

RewriteEngine On
 
# Redirect to subdirectory opw/
RewriteRule ^$ opw/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ opw/$1
 
# Force all non-https to https.
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,QSA]

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.