It’s odd, this sounds exactly like what I had for a couple years.
It would start with a Sharp pain around my ribs And then in the next day or so it would move to my back. My guess is that the back muscles worked too hard at compensating for the rib pain somehow.
Two main things helped. Stopped doing sit ups and most ab exercises. They triggered the rib issue pretty consistently.
Also starting take magnesium supplements. I feel it coming back sometimes if I miss a few days of supplements.
(Also you mention the hydration link, that sounds like it definitely could be an electrolyte issue. So magnesium for sure and also try to get more potassium)