_redirects not working with www subdomain

Hi all, running into an issue with my _redirects file and just wanted to see if anyone knows of a solution. I have some old URLs that I need to redirect to a new domain. I’ve setup redirects like this:

/api/* https://api.example.com/:splat

I noticed that when hitting my Netlify site without www, the redirect works fine. However, if using the www subdomain, the redirect doesn’t work. It feels like the bare domain and www should be interchangable when it comes to redirects, but that doesn’t seem to be the case.

I realize I could do full domains like this:

https://www.example.com/api/* https://api.example.com/:splat 200
https://example.com/api/* https://api.example.com/:splat 200

My annoyance with this approach comes from the fact that I can run this same app on different subdomains (beta build, etc), so for every redirect I want to add, I have to add at least 4 versions. That’s why I thought the regular /api/* path-only redirect would be ideal.

Is there a way to make this work the way I’m describing? Thanks in advance.

Hey @MikeBeas

Using

/api/*    https://api.example.com/:splat

should work regardless of the domain it is on (it would even work on the YOUR_SITE.netlify.app domain too—does it, out of curiosity?) as it does for the other sub-domains you have (beta, etc.)

If you can share the site in question and any other redirects you have in place that could help isolate a cause/solution.

Sure, the site is https://www.mikebeas.com.

This is my full redirects list:

/updatekit/selfupdate  https://api.mikebeas.com/shortcuts/updatekit 200
/updatekit/selfupdate.json  https://api.mikebeas.com/shortcuts/updatekit 200
/updatekit/update.json  https://api.mikebeas.com/shortcuts/updatekit 200

/pubkit/update.json  https://api.mikebeas.com/shortcuts/pubkit 200
/pubkit/update.json https://api.mikebeas.com/shortcuts/pubkit

/delorean/update.json https://api.mikebeas.com/shortcuts/delorean

https://www.mikebeas.com/*  /index.html 200
https://beta.mikebeas.com/*  /index.html 200
https://mkbs.me/*  /index.html 200
/api/* https://api.mikebeas.com/:splat
/gif/* https://mikebeas-gif.s3.us-east-2.amazonaws.com/:splat

Can you try switching the order or the redirects to this:

/api/* https://api.mikebeas.com/:splat
/gif/* https://mikebeas-gif.s3.us-east-2.amazonaws.com/:splat

https://www.mikebeas.com/*  /index.html 200
https://beta.mikebeas.com/*  /index.html 200
https://mkbs.me/*  /index.html 200
1 Like

That seems to have worked, and I see now why it would. Thanks for the help!

2 Likes