Skip to main content

How to Force SSL with htaccess

Updated 1 min readMarkdown
Part of seriesDevOps
How to Force SSL with htaccess

If you have a website running on apache2 and SSL certificate installed, but your visitors are landing on http connection sometimes, then you are in the right place.

Find your active htaccess file in the server. If there is not any, create one.

Use the below lines in the top of your htaccess file to force all users to visit your site with https

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

One important note: you need to enable ***RewriteEngine *** first.

sudo a2enmod rewrite

Thank you 👍😊

About the author

No comments yet. Be the first to comment.

More from this blog