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 http://your-domain.wtf to https://your-domain.wtf.

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

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

Comments

You might like

Add Dimensions to Image and Video Uploads in Laravel

Laravel

Calm: Peaceful Pictures

Opinion  ✺  Photography