Redirecting all domains to primary domain

Hi.

Please, I am trying to redirect all my domains to the primary domain but that doesn’t seem to work.
In simpler terms, suppose I have these 3 domains, app.example.com, web.example.com and example.com and I want to use example.com as my primary domain without losing traffic from the rest. How do I redirect all traffic to example.com? I have tried configuring the netlify.toml file as below but it doesn’t seem to work.

[[redirects]]
  from = "https://app.example.com/*"
  to = "https://example.com/:splat"
  status = 301

[[redirects]]
  from = "https://app.example.com"
  to = "https://example.com"
  status = 301

[[redirects]]
  from = "https://web.example.com/*"
  to = "https://example.com/:splat"
  status = 301

[[redirects]]
  from = "https://web.example.com"
  to = "https://example.com"
  status = 301

Hey @dozieogbo :wave:t2:

You’re very close! I do this on all of my sites as well. You just need to add a force = true and use the format from your first example.

[[redirects]]
  from = "https://app.example.com/*"
  to = "https://example.com/:splat"
  status = 301
  force = true

That, and make sure the domain you’re redirecting from is assigned to your site.

That should do it!


Jon

1 Like

Interesting.
I will try this and get back to you.

Hi @jonsully.
This doesn’t seem to work.
https://app.example.com still loads fine.

Hey @dozieogbo - Sorry to hear that :confused: can you double check that your netlify.toml file is in the root of your directory and that you don’t also have a _redirects file running in your publish directory? You should see on the build log for your site “X Redirect Rules Processed” as well. Can you check those things and let me know if that works?

It will also be a bit easier to help debug if you’re able to share the site name, URL, and your actual netlify.toml content as well :+1:t2:


Jon

1 Like

Hi @jonsully.
I can confirm that the X Redirect Rules Processed is showing up correctly and there’s no _redirects.
Can I share the actual Netlify.toml privately?

Interesting; especially that it’s showing having processed the rules correctly. If you’re following the format of

[[redirects]]
  from = "https://app.example.com/*"
  to = "https://example.com/:splat"
  status = 301
  force = true

and both domains are for sure assigned to that site, it should absolutely be redirecting. :thinking:

I’m more than happy to take a look at your netlify.toml content if you prefer to send me a private message here on the Community, but do be aware that while I am a Community Pilot, I am a volunteer and not officially affiliated with Netlify directly. :slight_smile:


Jon

1 Like

@dozieogbo Note that if you are using Netlify DNS, I think you should be able to do this in the DNS settings dashboard.

I am not using Netlify DNS @gregraven.

How do I send you a private message @jonsully

Thanks for your patience though.

Hi @jonsully and @gregraven.

I found what the issue was. I declared the SPA redirect route first before the domain redirects.

Declaring that after the code block posted in the question solves the issue. It now redirects perfectly.

Thanks a lot guys.
I am sure it would have been easier to resolve if I posted the actual file instead :man_facepalming:t6:.