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

In this example we consider the rewrite rules for Apache and Nginx to redirect a website from http to https ...

Home Programming Examples Other Examples → 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


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;
}



See More ExamplesHire Me For A Project






 
Connect with meLinkedIn ProfileFacebook Profile


2024 © SofiaCoder.com
×

Programming ExamplesPHP ExamplesMySQL ExamplesJavaScript ExamplesHTML ExamplesCSS ExamplesNode.js ExamplesOther Home PageSofia Coder LinkedIn ProfileSofia Coder Facebook Profile