UTIs occur when the urinary tract becomes infected, usually by bacteria. In most cases, bacteria from the gut enter the urinary tract through the urethra. This may happen when wiping your bottom or having sex. But often it's not clear why it happens.