How to switch a WordPress website to https using wp-cli?

Prerequisites:

  1. SSL certificate: Your website must have a valid SSL certificate installed and configured on your server. You can typically get an SSL certificate from your hosting provider.
  2. WP-CLI installed: WP-CLI should be installed and configured on your server. You can install WP-CLI using the following command:
curl -s https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar | sudo php -d allow_url_fopen=true -f

Step 1: Set up HTTPS redirects (.htaccess)

  1. Use the wp rewrite flush command to clear the WordPress rewrite rules:
wp rewrite flush
  1. Create a new file named .htaccess in the root directory of your WordPress website.
  2. Add the following rewrite rule to the .htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
  1. Save the .htaccess file. This rule will redirect any HTTP requests to HTTPS.

Step 2: Update WordPress URLs to HTTPS (Database)

  1. Use the following commands to update the WordPress URLs to HTTPS in the database:
wp option update siteurl https://www.your-domain.com
wp option update home https://www.your-domain.com

Step 3: Fix mixed content issues (Search-Replace)

  1. Use the wp search-replace command to replace all HTTP URLs with HTTPS URLs in the database and files:
wp search-replace http https: --all-tables --type=text --path=.
  1. This command will search and replace all occurrences of HTTP URLs with HTTPS URLs in your WordPress database and files.

Step 4: Verify HTTPS connection (Browser)

  1. Visit your website in a web browser. If you see a padlock icon in the address bar and the URL starts with “https://,” then your website is successfully using HTTPS.

Step 5: Test for mixed content issues (Regenerate Thumbnails)

  1. Generate new thumbnails for all images to ensure they are served over HTTPS:
wp media regenerate
  1. Manually check any remaining mixed content issues, such as embedded videos or scripts, and replace them with their HTTPS counterparts.

Leave a Reply

Your email address will not be published. Required fields are marked *