I've had this happen during long rides. I don't start getting it until around the century mark, and it doesn't always happen. My guess is that it's blood sugar related. Try eating a couple hundred calories of carbs immediately after the ride, and then keep eating over the next couple of hours. See if that helps. I have neither migraines nor headaches.
Another possibility is that your glasses aren't optically perfect. Try better/different glasses.
I don't get mrigaines but do wear polarized sunglasses that might not help.
The worst part is that it happens when I am still riding.
It does seem to related to long rides (60 to 100 miles). I have done these distances and more without any effect in the past and I am in good shape thanks in part to the mild winter. I will try to have a gel however the first time it happened I was being careful about getting enough caloriores.