Utis and pain

Okay ive had about three utis in the past 4-5 months and they hurt like hell when i try to have intercourse. Thats how i always know when i get one. But ive been taking the medication and when im done with it me and my partner are going to try again and see if it still hurts because the only tome that hurts is when hes inserting it.. Ugh it makes sex so ugh. But any tips how to stay away from getting utis.