Why does a custom domain redirect to herokuapp.com?
Asked Answered
C

3

6

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
Commonable answered 9/3, 2016 at 5:50 Comment(0)
C
7

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.

Commonable answered 14/3, 2016 at 17:2 Comment(3)
Thank you ! I overlooked that part, was too focused on the Domain part I didn't see the ENV variables ;)Yttria
@Yttria Glad this helped you :)Commonable
To get Chrome to play nicely, I had to flush Chrome's DNS cache (superuser.com/a/203702/252083) and sockets, along with the OS's DNS cache. Did that enough times and it worked. Thank you!Zak
F
1

For newer version of Ghost (3.x) while using the https://github.com/SNathJr/ghost-on-heroku deploy script, I updated the APP_PUBLIC_URL to use http://example.com.

Also I set a page rule in Cloudflare to use http://*example.com/* with the setting: Always use HTTPS

Frulla answered 7/3, 2020 at 19:19 Comment(0)
S
0

To follow up in a later date. I had the same issue. Using Heroku + Cloudflare + Custom domain. Andrei Erdoss answer works.

https in APP_PUBLIC_URL will cause 301 redirects.


For newer version of Ghost (3.x) while using the https://github.com/SNathJr/ghost-on-heroku deploy script, I updated the APP_PUBLIC_URL to use http://example.com.

Also I set a page rule in Cloudflare to use http://example.com/ with the setting: Always use HTTPS


Safekeeping answered 13/6, 2020 at 4:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.