Let's say my web can be accessed with www.example.com and example.herokuapp.com.

How can I redirect example.herokuapp.com/* to www.example.com/*?

Heroku suggests that I use HTTP status 301 Moved Permanently but doesn’t explain on how to do it.

  • stackoverflow.com/questions/18469742/… - Answered here. – L Martin Jul 12 '17 at 12:21
  • @Yhorian So I guess the answer is you can't do it on Heroku but you have to do within your own apps? – Wit Jul 12 '17 at 14:52
  • 2
    A 301 redirect is a HTTP header. It can be set by the server or the app. You've chosen to outsource the server architecture to Heroku. You only have one choice. – L Martin Jul 13 '17 at 8:07

A 301 Moved Permanently is a HTTP header sent from the server to the browser instructing the browser to redirect the user to the correct domain name, while at the same time telling search engines where the correct address is.

The way I have achieved this is to add logic to my Heroku app's to ensure the correct FQDN is being used. If it is using the herokuapp.com address then the 301 header is sent with the correct URI to go to (including URL path and query strings to ensure a seamless experience for the end user). The trick here is to make sure that if the user requests the URL appp1.herokuapps.com/folder1/page.html then they are 301 redirected to www.domain.com/folder1/page.html and not www.domain.com otherwise it can throw the entire user experience off as users expecting a particular page are instead being redirected to your homepage just because of the URL redirection needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.