Question

non-www redirect not working

Non-www redirect to www is not working on my server. Here is the following nginx configuration that I’m using. When I try on mobile or on a desktop browser it doesn’t connect and says webpage is unavailable. In mobile gives an DNS look-up failed.

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name example.ca;
        return 301 https://www.example.ca$request_uri;
}

server {

        server_name www.example.ca;

       
         listen 443 ssl http2 default_server;
         listen [::]:443 ssl http2 default_server;
         include snippets/ssl-www.example.ca.conf;
         include snippets/ssl-params.conf;

.
.


Subscribe
Share

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Have you setup a DNS an A-record for non-www also known as @ ? And since you’re using HTTPS, then you might want to redirect non-www for HTTPS too. If your certificate allows the non-www of course. And then we want to redirect www to HTTPS as well.

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name example.ca www.example.ca;
        return 301 https://www.example.ca$request_uri;
}
server {
        listen 443 ssl http2 default_server;
        listen [::]:443 ssl http2 default_server;
        include snippets/ssl-www.example.ca.conf;
        include snippets/ssl-params.conf;
        server_name example.ca;
        return 301 https://www.example.ca$request_uri;
}