How to force redirect HTTP to HTTPS in WordPress via htaccess

To force redirect HTTP to HTTPS in WordPress via htaccess, follow these steps:

  1. Connect to your website’s hosting account using an FTP client or File Manager.
  2. Locate the .htaccess file in the root directory of your WordPress installation. If you cannot see the .htaccess file, make sure that hidden files are visible.
  3. Open the .htaccess file in a text editor.
  4. Add the following code at the top of the file:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This code will redirect all HTTP requests to HTTPS.

  1. Save the changes to the .htaccess file and upload it to your server.
  2. Test the HTTPS redirection by visiting your website using HTTP. The browser should automatically redirect to the HTTPS version of your website.

It’s thinkable for a user to enter a direct HTTP URL on your WordPress site, even when an SSL certificate is active. To force any HTTP request to redirect to HTTPS, you can add the following code to your WordPress .htaccess file.

RewriteEngine On 
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
how-to-force-redirect-http-to-https-in-wordpress-by-htaccess

At last, There are given below final script for .htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


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

Note: Before making changes to your .htaccess file, it’s recommended to create a backup copy of the file in case anything goes wrong.

Leave a Reply

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