Codementor Events

Migration Issues: redirect www to non-www with GoDaddy, Azure and SSL

Published Feb 09, 2022
Migration Issues: redirect www to non-www with GoDaddy, Azure and SSL

Recently I worked on a project where we needed to migrate our app from .Net/Razor to Nextjs(React + Nodejs).
We migrated the code, rebuild the frontend in React/Nextjs and move the API to Nextjs API. We then tested the whole application. everything was running flawless in a local environment.
Unfortunately, while testing the production setup we encounter a major problem: The site was not redirecting anymore from www to non-www.
Seems an easy fix but it turned out to be rather challenging and it took me some time to spot the problem and find a stable and scalable solution.
Most of this time was spent finding the information online, therefore I want to share our journey here in the hope to save time to others developers.

The Problem

After we switched to the new system, our domain wasn’t redirecting the www website to the non-www anymore.

The Setup

Domain Provider: GoDaddy
Hosting solution: Azure Web App
Previous App: .Net application with Razor frontend framework
New App: Nextjs API (Nodejs) with Nextjs(React)

Investigating the problem

The first thing I did was check our domain provider, GoDaddy. We needed to be sure that the DNS were properly set.
After a quick check, I ascertain that the DNS for www was pointing to the main domain IP address, the non-www one. So all good here.

The second place where I looked for hints was the server. I was searching for redirect functions, a proxy, or any settings that could deal with possible redirection/forwarding of the www.
I couldn’t find anything, the server was set up in a very simple manner: www and non-www were set up as custom domains in Azure Web App and the Azure Web App IP address was properly added to GoDaddy DNS.

The last place where I looked into was the code.
Indeed here I found a redirect function. None of the people working on the project was aware of that function. Probably was written by a developer not working on the project anymore.
On top of that, I have no experience with C# or .Net and it took me some time to figure out what exactly the function was doing and most important if the function was used or not.
Seems the function was not used anywhere.

Problem analysis

Since I didn’t find any clear issues in the previous code and architecture, it was time to take a step back and analyze the problem outside our specific setup.
How does a www domain point to a non-www domain? What is the mechanism behind it?
www is simply a subdomain of a non-www domain.
How do we redirect a subdomain to a root domain?
This can be done at three different levels:

  • Domain/DNS level
  • Server level
  • Application level

The easiest option is probably the DNS level. For sure in our situation since most of the hosting providers offer a redirect service.
It was still not clear to me why it was previously working and now needed to have an extra redirect but the time was passing fast and we needed to find a solution.
Redirect with GoDaddy

In the DNS section of GoDaddy, under DNS Management, at the bottom of the page (at the time this article was written), there is a Forwarding section.
Here is possible to redirect a domain or a subdomain.
Bingo!

I add the www domain in the Subdomain field and the root domain in the Forward to field.
GoDaddy wants me to choose between http:// and https://.
I found it a bit odd but I thought: if I pick a secure connection, it should work for the non-secure too since the SSL certificate is set up on the Azure Web App.
And here is where I was wrong!
The forwarding was working but not for secure connections.
I kept playing around with the forwarding panel set up but no luck. The SSL certificate was missing and I couldn’t understand why.

After some research online, I realized we were not the only one with this problem; many other people had the same issue with GoDaddy specifically.
I therefore contacted GoDaddy through their forum (finding out how to contact them was also challenging which remind me how important it is to choose a service with good customer service, even if I end up paing more). Luckily we got a quick response.

Apparently, when GoDaddy forwards a domain or subdomain, this will pass through their server which doesn’t have an SSL certificate. And no, is not possible to install one.
This means that even if we have a certificate on our own server, it will not work since the forwarding goes through another server that doesn’t have the certificate.
You can see here the whole conversation:
https://community.godaddy.com/s/question/0D53t00006VmagOCAR/www-to-nonwww-doesnt-work-with-https

At this point it was clear I needed to find another solution.

Application level

Another option was to create a redirect function in Nodejs.
We use Nextjs framework which comes already with Nodejs server and this gives various possibilities:

  • Make a redirect on the next-config file
  • Redirect with Nextjs API
  • Create a custom node server and code a redirect function there

After some research, I eliminate the first two options since they don’t take into consideration the domain but just whatever comes after it.
This means that is possible to redirect or forward a page from /home to /new-home but at that point we already skip the domain and the protocol.
The only one aware of the domain is the server therefore a custom Nodejs server was the only option.
Nextjs docs recommend being careful with this solution since some important Nextjs functionality will be lost in the process.
Mh, this didn’t make me happy.
Another con of this solution was: A redirect is an operation that doesn’t concern the application in itself but mostly the domain.
If one day we will move away from Nextjs, we want the redirect to keep working.
I didn’t want a future developer to hit the same issue.

I was more and more convinced that the redirect must stay in the operation level of our architecture.

Server level

I started researching which Azure resource could be helpful to overcome this problem. The eligible candidates were:

  • Azure Frontdoor → Great and powerful but unfortunately too expensive for our use case. 20-25€ a month simply for a redirect is too much.
  • Azure Static Website or an empty Azure Web App that will handle the www and will take care of the redirect. → It felt too much hassle to create and maintain a whole app or website just for a redirect
  • Azure Serverless Function App → Create a serverless function that takes care of the redirect
  • Not Azure solutions but still valid: Use Cloudfair has a gateway. This solution is very much like Azure Frontdoor, but for free. The idea is to point the DNS names to Cluodfair and once there, add a rule to redirect to non-www. In this way, we would have done the same that GoDaddy was doing while avoiding the GoDaddy forward server and therefore the SSL certificate problem.

Based on those informations, I thought Azure Serverless Function and Cloudfair were the most valuable options in our case.

I decided to give a try to Azure Serverless Function and leave as the last option Cloudfare.
Even if Cloudfare is pretty easy to set up and I had no prior experience with serverless function, I wanted to avoid having an extra service to add complication to our pretty simple architecture.

And here my journey with Azure Serverless Functions App started!
Apparently, it is relatively easy to create an Azure Serverless Function if the app is on a Windows server, not so easy or at least requires more research if is a Linux server.
If you are interested, I wrote a step-by-step tutorial on how to create the redirect with Azure Serverless Functions on Linux server.

Conclusion

This was definitely a valuable lesson for me, even the easiest operation can turn out to be quite challenging.
This slowed down the rollout of the new system and created quite a lot of stress. I will not underestimate this task next time and this is also one of the reasons why I wrote this article: to remember.
I believe this issue could be caught earlier, probably with a bigger and more experienced team.

Discover and read more posts from giorgia sambrotta
get started
post commentsBe the first to share your opinion
Show more replies