Prerequisites:
- 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.
- 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)
- Use the
wp rewrite flush
command to clear the WordPress rewrite rules:
wp rewrite flush
- Create a new file named
.htaccess
in the root directory of your WordPress website. - Add the following rewrite rule to the
.htaccess
file:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
- Save the
.htaccess
file. This rule will redirect any HTTP requests to HTTPS.
Step 2: Update WordPress URLs to HTTPS (Database)
- 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)
- 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=.
- 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)
- 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)
- Generate new thumbnails for all images to ensure they are served over HTTPS:
wp media regenerate
- Manually check any remaining mixed content issues, such as embedded videos or scripts, and replace them with their HTTPS counterparts.
Leave a Reply