herokughost

Why does a custom domain redirect to herokuapp.com?


According to this Heroku documentation we can allow a Heroku app hosted at myapp.herokuapp.com to be accessed with a custom domain myapp.com.

I have added the follow records to my GoDaddy DNS Zone File:

A (Host)
Host        Points To                    TTL
--------------------------------------------------
@           50.63.202.1                  1/2 Hour


CName (Alias)
Host        Points To                    TTL
--------------------------------------------------
email       email.secureserver.net       1/2 Hour
ftp         @                            1/2 Hour
www         myapp.herokuapp.com          1/2 Hour

Normally the A record points to a server. In this case Heroku doesn't provide an IP address for its cloud applications, and apparently CNAME is enough.

Navigating to www.myapp.com or myapp.com redirects to https://myapp.herokuapp.com. However, navigating to http://www.myapp.com/blog/post/1 (any address not the homepage) will stay with the www.myapp.com domain, and clicking links is OK -- Except when clicking a link to the homepage we are redirected to the myapp.herokuapp.com address.

How can it be set up so the visitor does not see the herokuapp.com address unless visiting it with that address?

The output of heroku logs --tail --app myapp:

2016-03-11T01:13:49.756887+00:00 heroku[router]: at=info method=GET path="/" host=vast-hamlet-33090.herokuapp.com request_id=6177aa6c-dc5f-4de5-a1c6-1ff8b1194849 fwd="24.17.117.236" dyno=web.1 connect=1ms service=24ms status=304 bytes=181
2016-03-11T01:13:49.760014+00:00 app[web.1]: 24.17.117.236 - - [11/Mar/2016:01:13:49 +0000] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36"

It looks like the app never receives a request from www.myapp.com, but only myapp.herokuapp.com.

The output of heroku domains -a myapp:

=== myapp Heroku Domain
myapp.herokuapp.com

=== myapp Custom Domains
Domain Name         DNS Target
------------------  -------------------
www.myapp.com       myapp.herokuapp.com
myapp.com           myapp.herokuapp.com

Solution

  • In my case using Ghost, updating the HEROKU_URL in the Ghost configuration to 'http://www.myapp.com' fixed this error. I thought I had this set up, but I changed it to 'http://myapp.herokuapp.com' so I could access the admin panel, which wasn't accepting requests from URLs not HEROKU_URL.

    I was also having a separate issue with Chrome automatically, helpfully-not-helpfully redirecting before the request hit the server, so the Node app received a request for myapp.herokuapp.com. I discovered this by trying to use another browser to access my app. I cleared the browser cookies and cache and myapp.com was no longer redirected in Chrome.