I get a painful rash between my legs when I wear shorts. How can I prevent it?

Possibly friction. May be from friction causing the hair follicles to develop an irritation type folliculitis. Options include laser hair removal if this is the case and to avoid the friction by wearing longer shorts or applying something like vaniply or elta moisturizer to the area for lubrication as marathon runners do after miles of repetitive friction type movements.
Intertrigo. Intertrigo (intertriginous dermatitis) is an inflammatory condition of skin folds, induced or aggravated by heat, moisture, maceration, friction, and lack of air circulation. Intertrigo frequently is worsened or colonized by infection, which most commonly is candidal but also may be bacterial, fungal, or viral. Intertrigo commonly affects the axilla, perineum, inframammary creases, and abdominal f.