Skip to main content

Knowledge base

Redirect from http to https (or from https to http)

To configure a redirect from http to https upon accessing the website, you need to write a specific rule in the web server configuration file.

Apache

Open the .htaccess file to edit and append the following block:

RewriteEngine On
RewriteCond %{SERVER_PORT} ^443$ [OR]
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
To redirect from https to http, append the block:

RewriteEngine On
RewriteCond %{SERVER_PORT} ^80$ [OR]
RewriteCond %{HTTP} =on
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]

Nginx

Open the Nginx Web server configuration file for editing.

If you are configuring redirect settings for the first time, create the following sections in the file:

server {
    listen  IP-address:80;
    server_name  www.mydomain.com;
    rewrite ^ https://www.mydomain.com$request_uri? permanent; 
}


server {
    listen  IP-address:443;
    server_name www.mydomain.com;
.....
}

The first block is designed for opening the website via http (80 port) and directing the redirect, with the second block intended for opening the website via https (443 port), in the domain master section.

If the domain master section has already been created, remove the following line from it:

listen  IP-address:80;

and add a new section using:

server {
    listen  IP-address:80;
    server_name  www.mydomain.com;
    rewrite ^ https://www.mydomain.com$request_uri? permanent; 
}

In the event of setting a redirect from https to http, add sections:

server {
   listen  443;
   server_name  www.mydomain.com;
   rewrite ^ http://www.mydomain.com$request_uri? permanent; 
}




server {
   listen  80;
   server_name www.mydomain.com;
.....
}

If the second section (with master domain settings) exists, then make changes to it by adding these two lines.

Restart the Nginx Web server using the command:

nginx -t && nginx -s reload
Return to category