If your back or neck is killing you, or even if you have heartburn, sleep apnea or knee problems, the position you snooze in at night — on your tummy, flat on your back, curled up in a ball — can either make the problem better or worse.

The best sleeping positions depends on what ails you. Sleeping on your back is good for many physical ailments, but side sleeping is best for others, according to the Mayo Clinic, the American Pregnancy Association, the Better Sleep Council and Mary Ann Wilmarth, PT, DPT, a physical therapist in private practice outside Boston. Here is advice from Wilmarth and these organizations on how to wake up feeling relief.

Related: Can’t Lose Weight? Check Your Sleep

Hate the idea of abandoning your favorite sleep position? Wilmarth advises that before you go to sleep, you envision yourself waking up in your new position after a good slumber. It may take weeks, she says, but you'll eventually wake up that way.

Back or hip pain

Sleeping on your side is often best for back or hip pain, according to the National Sleep Council. That group and other experts advise pulling your legs slightly toward your chest and putting a pillow between your legs. Sleeping on your back can also be good for back pain, Wilmarth says, because it allows your spine to stay in natural alignment through the night — “but ideally, you want to raise your knees a little bit with a soft pillow so that your back doesn’t arch.” The Mayo Clinic notes this helps maintain the curve of your lower back. A pillow at the neck is critical, but don’t sleep on more than one pillow or you might strain your neck, Wilmarth says.

Acid reflux

Nighttime can be torture if you have a bad case of acid reflux, which can cause stomach acid to rise up into your esophagus. Sleeping on your left side is better for acid reflux than sleeping on the right side, according to an article in the journal Gastroenterology and Hepatology. If you sleep on your back, the authors note, raise the head of your mattress by putting a wedge pillow under it. Elevating the head of the bed “has been associated with significantly fewer and shorter reflux episodes, faster acid clearing and fewer reflux symptoms,” the authors conclude.

Related: Is it Heartburn, or Something Worse?

Shoulder or neck pain

If you have shoulder pain, it’s usually most comfortable to sleep on your back if you support the injured shoulder with a small pillow or towel. But don’t use more than one pillow, Wilmarth cautions, or you may wake up with a bigger crick in the neck. Also, try to keep your arms down rather than raised as if you were signaling for a right turn — that can lead to shoulder pain.

Wilmarth cautions against side sleeping for people with shoulder or neck pain. Studies in Australia have found that many people who sleep on their side end up in the fetal position, with their legs drawn up against their chest and their head and neck curved down, according to Wilmarth. "This can cause or aggravate neck and shoulder pain,” says Wilmarth.

Related: The Truth About Cracking Your Neck

Sleep apnea

The Mayo Clinic recommends sleeping on your side if you snore or have sleep apnea, a condition marked by loud snoring, gasping, waking up repeatedly and daytime drowsiness. If you can't keep yourself from rolling onto your back, try sewing a tennis ball or two into the back of your pajama top. Or try using an inflatable "anti-snoring" belt you wear around your chest, which makes it all but impossible to turn over, or putting pillows behind your back.

Knee pain and hip bursitis

Sleeping on your side with a thick pillow between your legs can help ease pain at night. Avoid sleeping on your stomach if you have knee pain, whether it’s from arthritis or an injury, Wilmarth says. Sleeping on your stomach can stretch out the back of the knee joint. “This can cause an overextension of the hamstring and knee joint, leading to pain in the knee or hamstring,” she explains.

Pregnancy

Pregnancy is not something that "ails" you, but it still matters how you sleep. The best sleep position during pregnancy is “SOS” (sleep on side), according to experts. Even better is to sleep on your left side, which may improve the flow of blood and nutrients to the baby according to the Mayo Clinic. That organization also suggests putting pillows between your bent knees, under your belly and behind your back.

The National Sleep Foundation points out that sleeping on your left side is also good for your kidneys because it helps eliminate waste from the body and thus reduces the hands and feet swelling that sometimes occurs during pregnancy.

Related: 8 Ways to Get the Sleep You Need for a Healthy Pregnancy

Kathryn Olney is a freelance writer and editor who has served as a reporter and editor for California, San Francisco and Mother Jones magazines.