Search code examples
http-redirectherokuhttpsnext.js

How to redirect a nextjs app hosted on Heroku from http to https?


I have a nextjs app hosted on Heroku. The app doesn't have a custom server, and visiting the https URL directly works fine.

However, if users visit the http URL directly, I'd like to redirect them to the https page.

What's the best way to achieve this these days?

There is a very hacky solution mentioned here, but I have the feeling that there is a better solution.

Any ideas?


Solution

  • You can use the Edge addon in Heroku which places a CloudFront CDN in front of your app which can handles the redirection. This enforces HTTPS i.e. Edge automatically redirects HTTP requests to HTTPS.

    Source: https://elements.heroku.com/addons/edge