Do dynamic stretches like leg swings etc before doing static stretches.
Stretch every day, drink lots of water. Stretch before and after working out, and also strengthen your leg muscles like your hamstrings.
Hip opening exercises ( google it ) are also very good.
I have a routine I do before classes of leg swings of different types, then jump rope, then static stretches, then jump rope and more dynamic stretches, then static. Muscles get more elastic as they get warmer, like a rubber band.