The knees are some of the most complicated joints of your body, this makes them very prone to injury. One common cause of it is the misalignment of the joint.
The knee is like a hinge that should open and close without any lateral movement, if muscle become unbalanced they could put abnormal pressure on the knee causing it to bend laterally, which is going to cause inflammation and even worst injuries like ligament and cartilage tears.
There is a simple way to self diagnose your knees alignment, your rotula should be pointing forward not be twisted internally facing each-other.
A way to make this very apparent is to stand with your feet shoulders width apart, make sure your feet are parallel with each-other, bend your knees.
If your knees are well aligned they will remain at equal distance from each-other if they are not they will bend toward each-other and sometimes even touch in the middle.
When attempting to rehabilitate this issue, the tendency is to address the knees and the muscles around the knees directly. Unfortunately this is rather ineffective at best offering some temporary relief.
Since your body is a complex system and the position of one joint is going to affect the position of another joint, it is impossible to have only misaligned knees.
One thing you will always find is a forward pelvis tilt, an over arched low back and a reverse curve in the neck. Because your body has to maintain equilibrium the upper body has to be balanced on the lower body. So when you go about correcting someone’s posture you always start with the highest mechanical compromised joint you can find.
Restoring that joint is going to cause all the joints underneath it to pop back into alignment on their own. Which is why your knee misalignment is not really a knee problem.
This make things rather counter intuitive, when your knee is hurting because your neck is tight, that often doesn't give any pain signals because there is no tissue damage in your neck but there is in your knee. So regardless that the problem is caused by your neck your knees are going to hurt your neck won't, but your neck is what needs to be stretched not your knees.
In particular the muscle in the back of your neck get really tight do to prolong sitting, that is going to cause a reverse curve in your neck, causing your centre of gravity to move forward. In an attempt to maintain equilibrium your body arches the lower back in order to move the weight of your head back over the heals so your body is going to be stable.
And if your lower back is going to be over arched also your pelvis is going to tilt forward, with the tilt of the pelvis your hips are going to rotate internally and feet and ankle externally. Which is what is causing your knees to be out of alignment.
In order to rehabilitate this the muscle in the back of your neck need to be stretched through chin to chest stretches like in the image below.
Standing look down and move your chin towards the chest until you feel the stretch in the back of your neck and maybe a little in your shoulder, it is not necessary to touch your chest with your chin, just move your chin towards the chest until you feel the stretch. Low intensity high duration works better when you are trying to change the lenght of your muscle so hold this for as long as possible.