Tuesday, November 7, 2017

[Tutorial] NGINX How to force SSL with WWW or without WWW, the clean way !


To force HTTPS without WWW you can use the following block in your server block :

        if ($host ~* www\.(.*)){
                set $host_without_www $1;
                return 301 https://$host_without_www$request_uri;
        }
        if ($scheme = http)
        {
                return 301 https://$server_name$request_uri;
        }


and to force HTTPS with WWW you can use the following block :

        if ($host !~* ^www\.){
                return 301 https://www.$host$request_uri;
        }
        if ($scheme = http)
        {
                return 301 https://www.$server_name$request_uri;
        }

One can save the above blocks in separate files (force_ssl.conf and force_ssl_www.conf) and simply include the conf files in their vhost servers whenever needed.

~ Mos

No comments:

Post a Comment

How to set up Kodi with YouTube addon on Raspberry OS Debian 12

 In this post, we review how to run Kodi with Kodi Youtube Addon on Raspberry Pi 5.  There are two versions of Kodi available on Raspberry P...