Forceful redirect from HTTP to HTTPS

HTTP download speed difference in windows 7 vs Linux
October 10, 2018
windows cannot find any valid language packs 2008 r2/ 2008 R2 standard edition doesn’t support language packs?
October 22, 2018

Forceful redirect from HTTP to HTTPS

Nginx

Below method can be used when you wish all configured sites to definitely only use https:

server {
              listen 80
              default_server;
              server_name _;
              return 301 https://$host$request_uri;
}

We can redirect only specific sites also. This is better if you have multiple apps/sites and not all of them should be forced to use SSL certificates.

server {
             listen 80;
             server_name yourdomain.com;
             return 301 https://$server_name$request_uri;
}

Apache

Redirect HTTP to HTTPS on Apache Using .htaccess File

For this method, make sure mod_rewrite is enabled

Once it is enabled, you can just add below lines to .htaccess in your domain’s document root directory:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

Redirect HTTP to HTTPS on Apache Virtual Host

To redirect HTTP to HTTPS for all the pages of your website, first open the appropriate virtual host file. Then modify it by adding the configuration below.

NameVirtualHost *:80
<VirtualHost *:80>
     ServerName www.yourdomain.com
     Redirect / https://www.yourdomain.com

</VirtualHost>
<VirtualHost _default_:443>
    ServerName www.yourdomain.com
    DocumentRoot /usr/local/apache2/htdocs
    SSLEngine On
</VirtualHost>

Save and close the file, then restart the HTTP sever

# sudo systemctl restart apache2 [Ubuntu/Debian OS]
# systemctl restart httpd [RHEL/CentOS OS]

Comments are closed.

%d bloggers like this: