Home > HTTP Apache > Redirect your wordpress site from http to https using .htaccess rule

Redirect your wordpress site from http to https using .htaccess rule

Once you have added an SSL certificate to your domain, the next thing that needs to configure your site is to forcefully redirect from HTTP to HTTPS. If your site has been configured using WordPress, then you can do the required changes using the .htaccess file.

.htaccess file is located at the main directory of the WordPress project, ie; in the primary directory of the website. It is a hidden file, so you may have to change the setting of the directory view to show all the hidden files.

Configuration to force HTTP redirection to HTTPS

  1. Navigate to the main directory of the project.
  2. Open the .htaccess in edit mode, if you are not able to open this file in the edit mode, please check the permission of the file. And if the .htaccess file is not there, then you will have to create a new .htaccess file in the main directory of the project.
  3. Add the below lines in the .htaccess file.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
Header always set Content-Security-Policy "upgrade-insecure-requests;"

4. Save this file and reload the page of your site in browser with http. If the above steps are successfully done, then the domain will redirect from http to https.

Complete content of the .htaccess file with the default WordPress setting code

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
Header always set Content-Security-Policy "upgrade-insecure-requests;"

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

The required lines of configuration were pasted just above the #BEGIN WordPress in the WordPress.

The above changes have been done by keeping in mind the SEO ranking of the site, using the 301 redirect SEO ranking of the site will now be affected. We have used 301 redirects in the above code.