I did the following:
-
I created a NextJS app by following the instructions as outlined in
the NextJS documentation. -
After this, I deployed this NextJS app with using Firebae App Hosting
(Public Preview) by following the instructions outlined on the
Firebase Documentation. -
Finally, I connected a custom domain (Purchased on GoDaddy) by
following the tutorial, also outlined in the Custom Domain
documentation (note the offical documentation do not mention any
specificities for GoDaddy).
This all worked fine, except for one thing, which is the http
-> https
redirect.
I could not find any resources that explain how this would work for a NextJS app specifically deployed on a Firebase App Hosting Setting. There are some resources that mention how to do it on the legacy Firebase Hosting, and also some suggestions on how to add something like this on the middleware, but both did not work for me (in the latter my middleware seems to be completely ignored):
if (request.headers.get("x-forwarded-proto") !== "https") {
return NextResponse.redirect(
`https://${request.headers.get('host')}${request.nextUrl.pathname}`,
301
)
}
What is therefore the recommended way to make sure that when an user visits http://example.com
it gets redirected to https://example.com
for a NextJS App Deployed on Firebase App Hosting of which the domain is purchased on GoDaddy.com?
2
Answers
I can reproduce your issue.
Firebase Hosting redirects all domains to HTTPS, Firebase App Hosting does also redirect the hosted.app domain (since .app is forced HTTPS). Custom domains don’t seem to be redirected.
The easiest solution would be to use a service as CloudFlare and create a Redirect Rule that way. The other solution, as you mentioned, is using regular Firebase Hosting in the meantime.
You can post a feature suggestion for Firebase here.
Purchase ssl cert from godaddy.
App Hosting can take up to a few hours to provision an SSL certificate for your domain after you update your DNS records.
You can view this certificate using the browser’s security tools. While the domain is provisioning, you might see an invalid certificate or have issues connecting to your domain. This is a normal part of the process and will resolve after your domain’s certificate is available.
Note: Firebase App Hosting automatically reprovisions SSL certs, as needed, for custom domains.