I have added two domains to my droplet (domain1.com and domain2.com)
I want to rewrite both www and non-www to domain1.com i.e.:
domain1.com => domain1.com domain2.com => domain1.com www.domain1.com => domain1.com www.domain2.com => domain1.com
It feels like I have tried every combination of server blocks in nginx but I cannot get it to work properly.
nginx version: nginx/1.4.6 (Ubuntu)
How do I set this up?
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!
Make sure that the “www” records exist for each domain: www CNAME @
Then, edit the current virtualhosts for your domain name and make sure server_name is set to domain.com and not www.domain.com.
Finally, add the following virtualhost for each domain:
server {
listen 80;
server_name www.domain.com;
return 301 $scheme://domain.com$request_uri;
}
Don’t forget to restart nginx after adding the virtualhosts.
Hi there,
I’ve tried the above and it just doesn’t seem to work for me. Can you explain to me what you mean by “Make sure that the “www” records exist for each domain: www CNAME @”?
Rashid.
I have currently a non-www domain, but would like to redirect the www to mine so I believe i have to add an entry to the CNAME field but not quite sure what values to insert. Anyone that could give me a helping hand?
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.