If the weather is cool, cover the knees. Wear tights or leg warmers even if you're not uncomfortably cold. Knee warmers can be easy to slipped on and off over cycling shoes to convert your shorts into knickers.
Spin in an easy gear for the first 10 minutes of your ride. Gradually increase your pace, avoiding hammering up hills for the first half hour of your ride. Don't push big gears, especially early in the season or in cold weather. Gear down for climbs: minimum rpms in a climb should be 75-80. Learn to spin in the 90's on the flats, which is much faster than novices pedal.
Get off your bike after the first ten minutes and stretch your hamstrings, calf muscles, and quads.
Go easy the last 10 minutes of your ride to cool down properly. This will wash toxins out of your muscles and allow you to cool down, which greatly improves your perfarmonce the next day.
At the gym use the leg curl machine to strengthen your hamstrings. Cyclists tend to overdevelop quads and under-develop hamstrings, leading to injury problems. Avoid the leg extension machine. It's not necessary for cyclists, is hard on the knees, and is really only useful for getting extra loft when kicking the cat after you loose a race.
Fit Your Bike
The bike frame needs to be the right size, you need the right size and rise of stem, size of handlebars, and proper adjustments to stem, seat post and seat rails, and shoe cleat positioning. A good rule of thumb: if the front of you knees hurts, raise the seat. If the back of your knees hurts, lower the seat. Don't move the seat height more than 1/8th inch per week if you can help it, to allow your body time to adapt. Some people have leg length inequalities that require shimming cleats: check with your orthopedist if you suspect this is a problem. He may be able to spot this problem quickly, or confirm it with an x-ray.
If it's Too Late...
If you feel leg strains coming on, try an anti-inflammatory such as Ibuprofen (Motrin/Advil), together with RICE: Rest the injury; Ice it (try 2 bags of frozen peas in a gallon ziplock bag) for 20 minutes on, 10 off); Compression (wrap in an elastic bandage); and Elevation (raise the injured part to reduce swelling). Though it may feel good at first, avoid hot tubs, spas and saunas for at least 72 hours. If pain persists, see your doctor.