redirect from a domain without "www" to the one with "www"

April 17, 2016 886 views

How can I setup my domain so that the main domain will be with "www" and when a user come to the bare domain without "www" they'll be redirected to the domain with "www" with the status 301 or 302? In the panel of namecheap I can do that, but I've changed the nameservers arlready so I have to do that here at digital ocean.

2 Answers

DNS (Nameservers) cannot cause HTTP Redirects. You have to do it at the webserver level. Separate the bare domain (without www) to a different vhost and have that vhost redirect to the primary vhost. You didn't specify what webserver you use so I can't help you more than that.

  • As I said, at namecheap I can do it without it while hosting my website somewhere else and namecheap doesn't know anything about my webserver, can I do such a redirect here as well?

    At namecheap I meant at the dashboard of my domain, at the domain level that is. Not at a hosting dashboard because I didn't host my website there.

    • When you did it with Namecheap, they probably hosted a tiny website for you, redirecting the address to the new one, even though you weren't hosting with them. It is impossible to do at the DNS Layer as DNS Servers don't even know what port or protocol you're querying for. - Only the hostname.

Have another answer? Share your knowledge.