Unfortunately, once a cat starts to relieve themselves outside of the litterbox there is no way to stop them from continuing this habit!!
You may want to contact your veterinarian to see if your cats have a health issue or if this has become a behavioral issue.
Cats can get kidney infections which cause this habit or they have just decided they don't want to use the litterbox due to filth issues of their box not being kept clean!!!
I would definitely suggest you contact your veterinarian to rule out any health problems!!!
You need to first figure out why your cat is refusing to use his box. It must be kept very clean because cats are very clean animals (just look at how often they groom themselves). Also, the location of the litterbox is very important to your cat. Make sure it isn't placed near his food dishes. Make sure the box is large enough for him. A big cat won't want to use a small box. If a large cat does try to use a box that's too small, he will not be able to "hit" his target. Have you changed the brand of litter lately? Could be he doesn't like it if it's scented when it previously wasn't, or he may not like it if it feels different on his feet. Also, if you have declawed him recently, his feet may be very tender now and the carpet feels very soft and more comfortable on his toes. Do you have more than one cat in the house and only one box they must share? Some cats don't like to do that. Some cats will even guard the litterbox to prevent another cat from using it. Add another box in another area. Is your cat old and has arthritis? Get a lower sided box to make getting in and out easier. If there are changes in your household that are causing stress in your cat's life, it can cause him to go elsewhere to relieve himself. As Quizmo stated, there maybe an underlying health issue. I had a cat that never went outside her box until one morning I heard her peeing beside my bed. She had gone on a newspaper that was lying on the floor and I could see blood in her urine. She had a severe UTI. She associated the pain she was feeling with using the litterbox, so she found another area. It could be many things as you see, but for sure you need to completely get rid of any trace of the odor in the carpet or you can will instinctly return to it. A good deodorizer is needed. Use a search engine to find a product. Natures Miracle, I hear, is supposed to be good. Good Luck!