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;

.
.



Submit an answer

This textbox defaults to using Markdown to format your answer.

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

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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;
}