NGINX redirect www to non-www

November 12, 2018 1.1k views
Nginx Ubuntu 16.04

Hi there, I am trying to redirect all NGINX www paths to non-www. Does anybody know how to do it?

server {
    root /var/www/mydomain.com/html;
    index index.html index.htm index.nginx-debian.html;

    server_name mydomain.com www.mydomain.com;

    #Certbot stuff here...
}

server {
    if ($host = www.mydomain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = mydomain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    listen [::]:80;

    server_name mydomain.com www.mydomain.com;
    return 404; # managed by Certbot
}

1 Answer

Hey. In connection with the latest news from Google, all sites need to be transferred to the secure HTTPs protocol. I still could not find the time to address this issue. Now I understand that until the end of the year there is little time left and it is necessary to transfer the site to a new protocol. My webmaster has left and does not get in touch. And I do not know how to make a website on the HTTPs protocol, such as they did an essay help site that I use. Access to ftp to change, access to the admin access panel is also there. But I did not find anything in the admin panel that suits me. Who can help me with this?

Have another answer? Share your knowledge.