There are a lot of things that might cause it, but if it only happens on long rides, I'd guess you aren't getting enough electrolytes. The more plain water you drink, the more your kidneys try to rid themselves of it to keep adequate electrolytes in your blood.

Do you use a sports drink, Endurolytes or similar - or make sure to eat plenty of salty snacks - when you're on long rides?

I am not a doctor, etc., etc....