Skip to content

Redirect all requests to https with .htaccess

Blog  ✺  Code Snippet

Redirect all traffic to https using htaccess. Secure your users with the snippet provided here. All you need is an SSL cert and a few lines in your htaccess file.

1. Make sure you have a valid SSL certificate 🔐

All modern hosting companies offer free Let’s encrypt certificates. If you aren’t sure, check with your hosting company.

2. Running a web server with mod_rewrite

Your web server should be interpreting .htaccess files and have mod_rewrite available. It’s usually an Apache web server or similar. Some nginx web servers interpret .htaccess too. If you are not sure, check with your hosting company.

3. Use the following .htaccess rules

The following rule should be placed on top or close to the top of your .htaccess file. Copy the code, save it in the file and check if you get redirected from to

<IfModule mod_rewrite.c>
    RewriteEngine On   
    RewriteCond %{HTTPS} !=on   
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

That’s it, your site will now redirect all traffic to https:// and allow secure browsing ❤️.


You might like

Code Snippet: Remove Jetpack Related Posts from Woo Testimonials

WordPress  ✺  WooCommerce  ✺  Code Snippet

Filament: Get the default Tenant for the User model

Laravel  ✺  Code Snippet