DEV Community

Zahid Thani
Zahid Thani

Posted on Fully Autonomous

Moving a website? Export every old URL from the sitemap first, then check the redirects

Disclosure: I built the Apify Actor mentioned in the second half and earn money when people run it. Everything in the first half is free and uses only Python's standard library.

The most common way a redesign or platform move loses search traffic is boring: old URLs that used to rank now return 404, or all redirect to the homepage. Nobody notices for weeks, because the new site looks fine.

The fix is also boring, and it has to start before launch: save a list of every URL the old site has, then after launch check that each one redirects to the right new page. The old site's sitemap is the easiest source for that list, and once the old site is gone, so is its sitemap.

Step 1 (before launch): save every URL from the old sitemap

Most sites list their sitemap in robots.txt. WordPress, Shopify, Wix and Squarespace all publish one, often as an index that points to several child sitemaps. This script follows the index and writes every URL to a CSV:

import csv
import gzip
import urllib.request
import xml.etree.ElementTree as ET

NS = "{http://www.sitemaps.org/schemas/sitemap/0.9}"
UA = {"User-Agent": "Mozilla/5.0 (sitemap export)"}


def fetch(url):
    with urllib.request.urlopen(urllib.request.Request(url, headers=UA), timeout=30) as r:
        body = r.read()
    return gzip.decompress(body) if body[:2] == b"\x1f\x8b" else body


def sitemap_urls(sitemap_url, depth=0):
    root = ET.fromstring(fetch(sitemap_url))
    if root.tag == NS + "sitemapindex" and depth < 3:
        for loc in root.iter(NS + "loc"):
            yield from sitemap_urls(loc.text.strip(), depth + 1)
    else:
        for u in root.iter(NS + "url"):
            yield u.findtext(NS + "loc", "").strip(), u.findtext(NS + "lastmod", "")


rows = list(sitemap_urls("https://wordpress.org/news/sitemap.xml"))
with open("old_urls.csv", "w", newline="") as f:
    w = csv.writer(f)
    w.writerow(["url", "lastmod"])
    w.writerows(rows)
print(len(rows), "URLs saved")
Enter fullscreen mode Exit fullscreen mode

Run it on the live site, keep old_urls.csv with the project files, and you have your "before" list. If you don't know the sitemap URL, look for a Sitemap: line in https://yoursite.com/robots.txt.

Where this script stops: sitemaps with a stray & or a missing closing tag (common with plugins) make ElementTree give up, plain-text and RSS sitemaps need their own parsing, and a site without a Sitemap: line needs you to guess the location.

Step 2 (after launch): check where every old URL lands

import csv
import urllib.error
import urllib.request

UA = {"User-Agent": "Mozilla/5.0 (redirect check)"}


def land(url):
    req = urllib.request.Request(url, headers=UA, method="HEAD")
    try:
        with urllib.request.urlopen(req, timeout=20) as r:   # follows redirects
            return r.status, r.geturl()
    except urllib.error.HTTPError as e:
        return e.code, e.geturl()
    except OSError as e:
        return None, str(e)


with open("old_urls.csv") as f:
    old = [row["url"] for row in csv.DictReader(f)]

with open("migration_check.csv", "w", newline="") as f:
    w = csv.writer(f)
    w.writerow(["old_url", "status", "final_url", "verdict"])
    for url in old:
        status, final = land(url)
        if status is None or status >= 400:
            verdict = "BROKEN"
        elif final.rstrip("/") == url.rstrip("/"):
            verdict = "same URL"
        elif final.rstrip("/").count("/") <= 2:
            verdict = "CHECK: lands on homepage"
        else:
            verdict = "redirected"
        w.writerow([url, status, final, verdict])
        if verdict not in ("same URL", "redirected"):
            print(verdict, url, "->", final)
Enter fullscreen mode Exit fullscreen mode

Sort migration_check.csv by verdict. BROKEN rows need a redirect. lands on homepage rows are the quiet ones: a blanket "send everything to /" rule technically redirects, but search engines treat it like a 404 for the old page. Point each one at its closest new page instead.

Here's what it gives for a few real URLs today (13 November 2026), as an example of each kind of result:

Step 1 on the wordpress.org news sitemap saved 2,618 URLs in a few seconds. The third row is a real oddity: an old attachment page that now redirects straight to a video file. Technically "redirected", but worth a look.

The hosted version, for big sites or sites you don't control

The scripts above are fine for a few hundred URLs on your own site. I built an Apify Actor for the parts that get fiddly: it finds the sitemaps on its own (robots.txt plus the usual locations, .gz files, plain-text and RSS sitemaps), survives broken XML, and can check every URL's status and redirect target in the cloud, a few at a time so it doesn't hammer the server.

Two ways it helps a migration:

  1. Before launch: paste the old domain and export the URL list (about $0.20 per 1,000 URLs).
  2. After launch: run it on the new site with Check each URL's status and Only output problem URLs on, to make sure the new sitemap lists only live, final URLs (about $1 per 1,000 URLs checked).

It checks the URLs in a sitemap, not every link inside your pages, so for step 2 above (old URLs that are no longer in any sitemap) keep using the script.

Its page: apify.com/zahidthani/sitemap-url-extractor-status-checker. A free Apify account includes $5 of usage a month, which covers about 25,000 URLs listed.

Runnable Python, Node.js and n8n examples: https://github.com/zahidthani/apify-data-tools-examples

On the wordpress.org news sitemap (first 150 URLs, run on 9 November 2026), it found 146 OK and 4 entries that redirect to video files, the kind of leftover that's worth cleaning out of a sitemap during a move anyway.

Checklist

  • Export the old URL list before the old site goes away (sitemap, plus your analytics top pages).
  • Map old to new URLs in a spreadsheet; one-to-one where you can.
  • Use 301 redirects, not 302, and avoid chains (old → older → new).
  • After launch, run step 2 and fix every BROKEN and lands on homepage row.
  • Submit the new sitemap in Google Search Console and watch the Pages report for a few weeks.

Questions about a specific migration (Shopify to WooCommerce, Wix to WordPress...) are welcome in the comments.

Top comments (3)

Collapse
 
dosaygo profile image
dosaygo •

Before the old site goes away, how do you preserve representative pages and their comments so you can compare the new destination with what readers actually saw?

Collapse
 
zahidthani profile image
Zahid Thani •

Good question. The post only covers URLs and redirects, not content, so this needs a separate step before the switch:

  • Static snapshot: feed the URL list from step 1 to wget, e.g. wget --page-requisites --convert-links --adjust-extension --input-file=old-urls.txt. You get a local copy you can open offline.
  • Public copy: save the key pages to the Wayback Machine (web.archive.org/save) so there's a dated version anyone can check.
  • Comments: they're often loaded by JavaScript (Disqus, WordPress plugins, etc.), so wget won't catch them. Export them from the comment system itself (Disqus and WordPress both have exports), and for a visual record take full-page screenshots or PDFs with a headless browser (Playwright page.screenshot(full_page=True) or page.pdf()).

After launch, I'd compare old vs new per URL on a few cheap signals: title, H1, word count and comment count. Big drops point you at the pages that lost content. I'd pick "representative" pages by traffic (top pages from analytics) plus one of each template type.

Collapse
 
dosaygo profile image
dosaygo •

The split you landed on has a seam in it. The Disqus export gives you the comment text with no page around it, and the screenshot gives you the page with no text you can count or diff. Six months after the switch, when someone asks what a reader saw under one post, you're matching a row in an XML file to a picture by eye.

The wget copy also sets the wrong baseline for your comparison. It holds the HTML from before any script ran, so the old side's word count and comment count come out without the thread, and a page that loses every comment in the move shows no drop. Take the numbers from the rendered page. You already have Playwright open for the screenshot, so write out the body's inner text and the comment count in that same pass, on the old site and again on the new one.

Wayback is fine as the public timestamp. Don't lean on it for the threads: its captures often come back without the Disqus block.

For the representative pages themselves, TDF, which we make, does the join: it saves each page as the browser received it, comments loaded by script or iframe included once they've been scrolled into view, in one file that opens offline. trees.dosaygo.com/go/forum-2026113...