Redirect a Default Netlify Subdomain to Your Custom Domain

When I first set up a custom domain for an app/build with Netlify, I did not realise straight away that the default (or custom) subdomain that Netlify had used originally to host my app (e.g., <appID>.netlify.com) was still returning the same version of the website.
For example, whilst building the IMG Licensing website, the application's original URL back in 2020 was jovial-kowalevski-54108f.netlify.com, we wanted to make sure that when the website went live, we did not have a situation where exactly the same content was being hosted there, as from the primary domain: imglicensing.com.
Whilst for many people this duplication of the site (and content) isn't a problem, what this can lead to is search engine indexing issues where a crawler finds two sources with similar or identical content, and may choose a canonical URL other than the one you intended. It's a problem I've dealt with a lot in the past where people have copied chunks of my personal website or projects.
This isn't an automatic duplicate‑content penalty. A canonical tag helps search engines choose your preferred URL, but visitors still see whichever address they opened. When you want the default hostname to stop serving the site, a permanent redirect is the more useful fix. Keep the destination's canonical consistent with it.
The solution ‑ however ‑ is very straightforward. Netlify's redirect documentation covers the options, but all we need here is a file called _redirects in the root of the deployed site. The current default hostname ends in .netlify.app, and connecting a custom domain does not disable it. Use the exact Netlify hostname shown for your project.
If you're developing in Gatsby, create the file, put it in your static folder, and use the code below to set up your subdomain redirect:
# Redirect all traffic from your Netlify subdomain to your primary domain:
https://yourproject.netlify.app/* https://yourwebsite.com/:splat 301!Using those old project names to illustrate today's syntax (not a working demo), the rule would look like this:
https://jovial-kowalevski-54108f.netlify.app/* https://imglicensing.com/:splat 301!Keep this hostname rule before catch‑all rewrites or other rules that would match it. Netlify uses the first matching rule and reads _redirects before netlify.toml. If you already keep redirects in netlify.toml, the equivalent [[redirects]] uses from, to, status = 301, and force = true; there's no need to duplicate it in both files.
Commit this to your repo and deploy the site, then check the redirect once the deployment finishes.
A couple of small details worth mentioning:
- The wildcard (
*) captures the path and:splatputs it back at the destination. Leave:splatout and every matching page lands on the homepage. Netlify also passes query parameters through for this301, so/about?ref=testbecomes/about?ref=teston your custom domain. 301makes the move permanent. The!forces the rule even when a file exists at that path, which matters when the whole point is to stop serving the same pages under the old hostname. It doesn't override an earlier matching rule.- Keep the full source hostname. A rule starting with only
/*would also match your custom domain and could loop. The exact hostname leaves distinct Deploy Preview and branch‑deploy addresses alone.
Choose the final HTTPS destination, including the right www or non‑www host, to avoid an unnecessary extra redirect. These rules match HTTPS requests; check HTTP too, and add the equivalent http:// source rule if you haven't enabled automatic HTTPS redirection. If an Edge Function responds before Netlify reaches the redirect rules, the ! won't bypass it.
Check It Before Calling It Done
After the deployment finishes, substitute your own hostname and a real page in these commands:
curl -I "https://yourproject.netlify.app/articles/example?ref=test"
curl -IL --max-redirs 5 "https://yourproject.netlify.app/articles/example?ref=test"The first command shows the initial response: expect 301 and a Location header with your custom hostname, /articles/example, and ?ref=test. The second follows the redirects, up to five hops, so you can spot loops, a wrong destination, or an unnecessary trip through another hostname. Netlify can normalise trailing slashes before matching, so check the actual response rather than assuming every extra hop is this rule.
Finally, request the destination directly and confirm the page loads. A preview URL should still open the preview. If the default host serves the page, check that _redirects reached the deployed root and that nothing intercepts the request before this rule.
Fin.
Postscript
November 2026: I've brought the examples up to date with Netlify's .netlify.app addresses and added checks for query strings and competing rules. The original Gatsby project is historical, but the redirect still works this way. I'd check it alongside the canonicals and sitemap in my technical SEO launch checklist.