How to Redirect to HTTPS using .htaccess With WordPress

HTTPS encryption safeguards the data transmitted between your WordPress website and its visitors, protecting browsing and sensitive information. This includes login credentials, personal details, and payment information. It also fosters trust in your website and brand. Additionally, search engines like Google prioritize HTTPS websites, potentially boosting your website’s search engine rankings.

Configuring HTTPS Redirection for Your WordPress Website

SSL should already be enabled on your website and accessing with HTTPS should work. Many web hosting providers offer free SSL certificates through services within cPanel such as AutoSSL and Let’s Encrypt.

In addition to manually configuring HTTPS redirection using the provided code snippet, you can also opt for the convenience of using a plugin such as “Really Simple SSL.” This plugin simplifies the process of forcing HTTPS on your WordPress website by automatically detecting your SSL handling the necessary settings for HTTPS redirection.

  • Navigate to your hosting provider’s File Manager tool. This tool allows you to access and edit your website files, including the .htaccess file for HTTPS redirection.
  • Find the .htaccess file in the root directory of your WordPress installation, typically named “public_html.” Before editing, copy the file to the current directory with a new name to revert to in case of errors.
  • Open the .htaccess file for editing and insert the following code snippet at the beginning of the file:

    RewriteCond %{HTTPS} !on
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

    The code snippet above uses Apache’s mod_rewrite module to enforce HTTPS redirection. It first checks if HTTPS is not already enabled (RewriteCond %{HTTPS} !on) and then verifies if the X-Forwarded-Proto header does not contain ‘https’ (RewriteCond %{HTTP:X-Forwarded-Proto} !https). If both conditions are met, it redirects the request to the HTTPS version of the https version and keeping any trailing URL.
  • After adding the code, save the changes to the .htaccess file. It’s crucial to double-check for any typos or mistakes, as an incorrect .htaccess configuration can lead to errors on your website. To test the HTTPS redirection, you should access your WordPress website in a new tab or incognito window to clear any cache. Also use the standard HTTP protocol (http://yourwebsite.com) to make sure it redirects automatically to https.

Securing your WordPress website with HTTPS and redirecting HTTP traffic to HTTPS is vital in safeguarding your users’ data and building trust with your audience. By obtaining an SSL certificate and following the step-by-step guide for cPanel users, you can ensure that your website enjoys the benefits of a secure browsing experience. Embrace HTTPS today to enhance your website’s security, protect your users, and improve your online reputation.