Postherpetic neuralgia (PHN) is a refractory neuropathic pain condition in which new treatment options are being developed. Repetitive transcranial magnetic stimulation (rTMS) may have the potential to reduce pain sensations in patients with postherpetic neuralgia. This study investigated the efficacy on postherpetic neuralgia by stimulating two potential targets, the motor cortex (M1) and the dorsolateral prefrontal cortex (DLPFC). This is a double-blind, randomised, sham-controlled study. Potential participants were recruited from Hangzhou First People's Hospital. Patients were randomly assigned to either the M1, DLPFC or Sham group. Patients received ten daily sessions of 10-Hz rTMS in 2 consecutive weeks. The primary outcome measure was visual analogue scale (VAS) assessed at baseline, first week of treatment (week 1), post-treatment (week 2), 1-week (week 4), 1-month (week 6) and 3-month (week 14) follow-up. Of sixty patients enrolled, 51 received treatment and completed all outcome assessments. M1 stimulation resulted in a larger analgesia during and after treatment compared to the Sham (week 2 - week 14, p < 0.005), as well as to the DLPFC stimulation (week 1 - week 14, p < 0.05). In addition to pain, sleep disturbance was significantly improved and relieved by targeting either the M1 or the DLPFC (M1: week 4 - week 14, p < 0.01; DLPFC: week 4 - week 14, p < 0.01). Moreover, pain sensations following M1 stimulation uniquely predicted improvement in sleep quality. M1 rTMS is superior to DLPFC stimulation in treating PHN with excellent pain response and long-term analgesia. Meanwhile, M1 and DLPFC stimulation were equally effective in improving sleep quality in PHN. https://www.chictr.org.cn/, identifier ChiCTR2100051963.