In general, one should be very careful taking medication to avoid the side-effects of red wine or alcohol in general. Generally, it is wise to search the advice of a doctor on the subject.
Dehydration is one of the main causes of headache from wine or other types of alcohol (except from beer). One good advice is to drink two glasses of water for each glass of wine.
One of the possible causes of RWH, is the release of prostaglandins, which some people are not able to metabolize. By taking a dose of prostaglandin inhibitors, before drinking, some people experience a good effect. Aspirin, Ibuprofen and Tylenol are examples of such medication/painkillers.
Histamines is a main culprit in causing headache. Accordingly, antihistamines such as Cetirizine are relevant when discussing how to deal with the problem.
To be perfectly safe, if the problem of headache and migraine is all the time re-occuring after having tried out even the most suitable wines on the market, is of course to drink non-alcoholic beverages.