WordPress HTTPS

Migrating WordPress or WooCommerce from HTTP to HTTPS

I just migrated our company website which uses WordPress and WooCommerce from HTTP to HTTPS following the basic guidelines of Move a site with URL changes in Google’s Webmaster Tools help. From Google’s perspective the following scenarios require the same approach:

  • URL changes from HTTP to HTTPS
  • Domain name changes such as example.com to example.ch or merging multiple domains or hostnames
  • URL paths changes: example.ch/page.php?id=1 > example.ch/widget, or example.ch/page.html > example.ch/page.htm

Here are the 3 relatively simple steps I took to migrate the entire site.

1. Backup Database

Make a backup of your database and check if the dump file is complete. Personally, I use the mysqldump utility directly on the server which makes it very easy and fast to restore the database with the mysql utility in case something goes wrong. If you’re not into mysql command line utilities, there’s of course always phpMyAdmin or various WordPress plugins to do the job.

2. Change Site URL

You could change the site URL in your wp-config.php by setting the following to constants

define('WP_HOME', 'https://www.yourdomain.ch');
define('WP_SITEURL', 'https://www.yourdomain.ch');

But In order to do it the clean way I searched and replaced http://www.yourdomain.ch with https://www.yourdomain.ch in the entire database using Search and Replace DB instead of searching and replacing it in the MySQL dump file directly which I used to formerly do with sometimes unexpected results. Search Replace DB allows you to carry out database wide search/replace actions that don’t damage PHP serialized strings or objects with a user friendly web-based interface.

Now you can already browse your entire site with HTTPS, but in order to redirect users coming from HTTP URLs indexed in search engines and users entering your site’s home page using HTTP, there’s another step required.

3. Add Rewrite Condition

Depending on your web server, you can add rewrite conditions either directly in your virtual host configuration or in shared hosting environments where you don’t have full access, you will have to use .htaccess. Add the following directives before the default WordPress rewrites.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

If your browser does not return a 500 internal server error you should be able to see a 301 HTTP header with Firebug in Firefox or using the Live HTTP Headers plugin for Chrome when following an HTTP URL from a Google search result page. The 301 header will not only make sure that visitors will be automatically redirected from HTTP to HTTPS, but it will also tell search engines to update the index. That’s it, you should be all set. Don’t forget to monitor the traffic on both the old and new URLs in order to make sure everything works properly.

Kommentar verfassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert