how to redirect http to https or fix the not secure warning in the browser

How to redirect http to https or fix the not secure warning in the browser


Navigation


I often got questions from customers asking why their website is showing a Not Secure warning in the browser when they have a SSL installed etc.
In general if you have a valid SSL installed, the second possible reason is that the website is opened with http:// instead of https:// and in this post, I'll show you 2 ways to redirect automatically all http:// requests to https:// (in order that the browser doesn't produce such warnings) for Apache with rewrite rules and also for the nginx server.

For Apache, if you currently don't have a .htaccess file for you site, you may create one or if you already have it, add such rewrite rules at the top -


RewriteEngine On

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://yourdomain.com/$1 [R=301,L]


(by replacing yourdomain.com with your current domain name)

Basically we check here if the website visitor is connecting to the website using port 80 (not secure connection / http), we'll redirect it to the same URL, but now with https://

An alternative way to do it, are the following rewrite rules -


RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


For Nginx, it's necessary to locate the configuration file for your website (usually found in the /etc/nginx/sites-available/ directory) and open it in a text editor.

Within the server block for the HTTP version of your website, you may add the following lines:

server {
listen 80;
server_name your-domain.com;
return 301 https://$host$request_uri;
}