In today’s internet landscape, security is more important than ever. One of the easiest and most effective ways to ensure that your website is secure is by implementing HTTPS (Hypertext Transfer Protocol Secure). HTTPS encrypts the data sent between the server and the client, ensuring that sensitive information such as passwords, personal data, and credit card details are transmitted securely. Websites that use HTTPS are favored by search engines and gain a ranking boost, while users are reassured by the “padlock” icon in their browser’s address bar.
For WordPress websites, forcing a redirect from HTTP to HTTPS can be done quite easily using the .htaccess file. The .htaccess file is a configuration file used by Apache servers to control various settings, such as URL redirects, security rules, and more. In this blog post, we’ll walk you through the process of forcing HTTP to HTTPS in WordPress via .htaccess, including the necessary code and steps.
Why Use HTTPS and Force Redirect HTTP to HTTPS?
Before diving into the technical aspects of forcing HTTPS, let’s briefly discuss why you should make the switch to HTTPS if you haven’t already:
1. Security
The most obvious reason for using HTTPS is security. It encrypts data exchanged between the server and the client, ensuring that malicious actors can’t intercept or tamper with sensitive information. This is especially critical if your website handles personal information or processes transactions.
2. Search Engine Optimization (SEO)
Google uses HTTPS as a ranking signal. Websites that use HTTPS may rank higher in search results compared to their HTTP counterparts. By forcing HTTPS, you’re ensuring that search engines like Google index the secure version of your website.
3. Trustworthiness and User Confidence
Users are more likely to trust websites that use HTTPS, especially if they see the padlock symbol in their browser’s address bar. Without HTTPS, browsers like Google Chrome mark HTTP sites as “Not Secure,” which can discourage visitors from interacting with your site.
4. Browser Compatibility
Most modern browsers now give warning messages when users visit HTTP sites. These warnings often make users hesitant to continue browsing. By redirecting HTTP to HTTPS, you avoid these warnings and ensure a seamless user experience.
Prerequisites:
Before we proceed, ensure that you have the following in place:
- SSL Certificate: To enable HTTPS, you must have an SSL certificate installed on your website. Many web hosting providers offer free SSL certificates through Let’s Encrypt. You can also purchase SSL certificates through various providers like Comodo, Symantec, or GlobalSign.
-
Access to .htaccess File: The
.htaccessfile is located in your WordPress installation’s root directory. You need to have access to this file in order to configure the necessary redirect rules.
Steps to Force HTTP to HTTPS in WordPress via .htaccess
Once you have your SSL certificate and access to the .htaccess file, the next step is to force a redirect from HTTP to HTTPS. Here’s how you can do it:
1. Locate the .htaccess File
The .htaccess file is typically located in the root directory of your WordPress installation. You can access this file via FTP, SFTP, or through the file manager in your web hosting control panel (cPanel, Plesk, etc.).
If you don’t see the .htaccess file, it might be hidden by default. You can enable hidden files in your FTP client or file manager to make it visible.
2. Back Up Your .htaccess File
Before making any changes, always back up your .htaccess file. This is a precautionary step in case you make an error, and you need to restore the original file.
3. Edit the .htaccess File
Once you’ve located the .htaccess file and made a backup, you can begin editing it. Open the file in a text editor and add the following code to force a redirect from HTTP to HTTPS.
<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>
Explanation of the Code:
1. <IfModule mod_rewrite.c>
This line checks if the mod_rewrite module is enabled on the server. The mod_rewrite module is necessary for URL rewriting (which is how we handle the HTTP to HTTPS redirection).
2. RewriteEngine On
This line enables the rewrite engine, which is required for the rewrite rules to take effect.
3. RewriteBase /
The RewriteBase directive is used to specify the base URL for the rewrites. In most cases, this should be set to /, as it applies to the entire site.
4. RewriteCond %{HTTPS} !=on
This line checks if the request is not using HTTPS. The %{HTTPS} variable holds the current connection’s protocol. If it is not set to “on” (meaning the request is using HTTP), the condition evaluates as true, and the next rule is applied.
5. RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
If the above condition is true (i.e., the request is using HTTP), this rule redirects the user to the HTTPS version of the same URL. The R=301 flag signifies that this is a permanent redirect (301), and the L flag tells Apache to stop processing further rules if this rule is matched.
6. # BEGIN WordPress
This comment marks the beginning of WordPress-specific rewrite rules. WordPress automatically adds its own rewrite rules here, which are responsible for handling permalinks and other URL rewrites.
7. RewriteRule ^index\.php$ - [L]
This rule ensures that any requests for index.php are handled by WordPress and not redirected or rewritten.
8. RewriteCond %{REQUEST_FILENAME} !-f and RewriteCond %{REQUEST_FILENAME} !-d
These conditions check if the requested file or directory exists on the server. If the requested resource is not a valid file or directory, the request will be sent to WordPress’s index.php file.
9. RewriteRule . /index.php [L]
This rule ensures that any requests for non-existent files or directories are routed to WordPress’s index.php file for processing.
4. Save the .htaccess File
Once you’ve added the code, save the .htaccess file. After saving, you’ve successfully configured your website to force a redirect from HTTP to HTTPS.
5. Test the Redirect
To ensure that the redirect is working, visit your website using http:// and see if you are automatically redirected to the https:// version of the page. For example, visit http://www.yoursite.com and check if it redirects to https://www.yoursite.com.
If the redirect works as expected, your site is now fully set up to use HTTPS.
Common Issues and Troubleshooting
While forcing the HTTP to HTTPS redirect via .htaccess is generally straightforward, there are some issues you may encounter. Here are a few common problems and solutions:
1. Infinite Redirect Loop
An infinite redirect loop can occur if there are conflicting rules in the .htaccess file. Make sure that you don’t have any other redirect rules that conflict with the ones we added.
2. Mixed Content Issues
When you switch to HTTPS, you may encounter mixed content issues, which happen when some resources (like images, scripts, or stylesheets) are still loaded over HTTP. To fix this, you can:
- Update your WordPress settings to use
https://for the site URL and home URL. - Manually update URLs in your content or database.
- Use a plugin like “Better Search Replace” to replace all instances of
http://withhttps://.
3. SSL Certificate Not Installed
If your SSL certificate is not installed properly, the browser may show a security warning even if the redirect is working. Ensure that your SSL certificate is correctly installed by checking with your hosting provider.
Conclusion
Forcing a redirect from HTTP to HTTPS in WordPress is a crucial step to ensure the security, SEO, and user confidence of your website. By modifying the .htaccess file, you can easily ensure that all visitors are redirected to the secure HTTPS version of your site. The code we provided is simple to implement and highly effective.
Once you’ve made the change, be sure to test it thoroughly and check for any issues such as mixed content. If you encounter any problems, refer to the troubleshooting section to resolve common issues.
By following these steps, you’ll enhance the security of your website, improve its search engine ranking, and provide a better experience for your users.

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