I’m using Heroku, and have added a couple custom domains for my app, i.e. myapp.com and www.myapp.com.
My DNS at GoDaddy has three A records for ‘@’ pointing to three separate Heroku IPs, and a CNAME for the ‘www’ subdomain that points to proxy.heroku.com.
What I want to do is redirect any traffic to www.myapp.com to myapp.com. I tried setting the CNAME to ‘@’, but that still remains at the same domain. Is there a way I can force this redirect at the DNS level?
CNAME is not a redirect but only a canonical name for your domain. That means that it behaves just like the domain it points to (myapp.com in your case). Your browser gets the same IP address as myapp.com has and sends a request to it.
Redirects are performed at the HTTP level or above. You can do this for example in your app or create another simple app just for that.
Here’s a simple example to do the redirect directly in your app:
Or you could use rails metal, which would do the same, but much faster:
You could also use a Regex to match all requests with
www.in front of the hostname.