Report this

What is the reason for this report?

Redirecting Traffic from www.domain.com to domain.com

Posted on April 1, 2019

I’ve decided to use the one-click Ghost install on digitalocean, and I like how easy it has made the process. However, I am having an issue with my site. I want to redirect the www.domain.com to domain.com, but my config doesn’t seem to work.

I have Letsencrypt certs for my domain which is arsalan.io, and it works fine. It gives a cert error if you try www.arsalan.io, so since I don’t want people browsing to www, I’d rather just have it forwarded to arsalan.io. I’ve setup www as a CNAME that points to the main domain.

My NGINX config is as follows (/etc/nginx/sites-available/arsalan.io.conf):

server {
    listen 80;
    listen [::]:80;

    server_name arsalan.io;
    root /var/www/ghost/system/nginx-root;

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:2368;

    }

    location ~ /.well-known {
        allow all;
    }

    client_max_body_size 50m;
}

I’ve tried modifying it so it looks like the following but it doesn’t redirect:

server_name arsalan.io www.arsalan.io;
redirect 301 https://arsalan.io$request_uri;

I haven’t modified arsalan.io-ssl.conf



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!

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.

Because you want to redirect a request from www.arsalan.io to arsalan.io you want to create a new server directive that is listening for connections on just www.arsalan.io and then add the redirect.

So you would need to update your configuration with the following:

server {
    server_name www.arsalan.io;
    return 301 $scheme://arsalan.io$request_uri;
}

In the snippet that you posted you had both server_names present, which may have been causing the issue.

You can find a more detailed write here: How to redirect with Nginx

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.