DEV Community

Sushil Barwal
Sushil Barwal

Posted on

Build a WooCommerce to Shopify redirect map with 40 lines of Python

When a WooCommerce store moves to Shopify, nearly every URL changes. WooCommerce lets you shape permalinks; Shopify doesn't. Products always live under /products/, collections under /collections/, pages under /pages/ and posts under /blogs/<blog-name>/. Shopify treats those prefixes as fixed.

That means the SEO side of a migration comes down to one file: a list of every old URL that gets traffic or has links, each pointing at its new Shopify address with a 301. Shopify's URL redirect import takes a CSV with two columns, Redirect from and Redirect to, so the job is to produce that file and check it before launch.

Here is the small script we use as a starting point, and the checks we run around it.

Step 1: collect the old URLs

Don't rely only on the sitemap. Combine three sources:

  • The WordPress sitemap (Yoast, Rank Math or core wp-sitemap.xml).
  • Search Console: Performance → Pages, last 16 months, exported to CSV. These are the URLs Google actually shows.
  • Your backlink tool or Search Console's Links report: pages other sites link to.

Put them all in one text file, one URL per line, and de-duplicate.

Step 2: map them by pattern

WooCommerce's default bases are /product/ and /product-category/. Check Settings → Permalinks in WordPress first, because many stores change them (/shop/, /item/, /category/), and adjust the patterns below.

import csv, re, sys
from urllib.parse import urlparse

RULES = [
    (r"^/product/([^/]+)/?$",                lambda m: f"/products/{m[1]}"),
    (r"^/product-category/(?:.+/)?([^/]+)/?$", lambda m: f"/collections/{m[1]}"),
    (r"^/product-tag/([^/]+)/?$",            lambda m: f"/collections/{m[1]}"),
    (r"^/shop/?$",                           lambda m: "/collections/all"),
    (r"^/\d{4}/\d{2}/([^/]+)/?$",             lambda m: f"/blogs/news/{m[1]}"),
    (r"^/(about-us|contact|shipping|returns)/?$", lambda m: f"/pages/{m[1]}"),
]

def map_path(path):
    for pattern, target in RULES:
        m = re.match(pattern, path)
        if m:
            return target(m)
    return None

def main(infile, outfile):
    unmapped = []
    with open(outfile, "w", newline="") as f:
        w = csv.writer(f)
        w.writerow(["Redirect from", "Redirect to"])
        for line in open(infile):
            path = urlparse(line.strip()).path or "/"
            if path == "/":
                continue
            new = map_path(path)
            if new:
                w.writerow([path.rstrip("/") or "/", new])
            else:
                unmapped.append(path)
    print(f"{len(unmapped)} URLs need a manual decision:")
    print("\n".join(unmapped))

if __name__ == "__main__":
    main(sys.argv[1], sys.argv[2])
Enter fullscreen mode Exit fullscreen mode

Run it with python map_redirects.py old-urls.txt redirects.csv. Everything it can't match is printed so a person decides where it goes. That list is usually short but important: landing pages, old campaign URLs and posts without a date in the path.

A few notes on the patterns:

  • Nested categories flatten. /product-category/clothing/hoodies/ becomes /collections/hoodies, because Shopify has no nested collection paths. If two child categories share a slug under different parents, the script will map them to the same collection: check for duplicates.
  • Tags only map if you actually build matching collections. Otherwise send them to the nearest real collection.
  • Blog posts land under /blogs/news/ because Shopify's default blog is called News. Change it if you create a different blog.
  • Keep slugs identical where you can. If blue-hoodie stays blue-hoodie, the map is easy to read and easy to audit.

Step 3: check the targets exist

A redirect to a 404 is worse than no redirect. Before importing, request every target on the Shopify development store (with the storefront password, or from an admin session) and flag anything that isn't a 200. If you used Shopify's Store Migration app or a CSV import, remember imported products start as drafts, so publish them on the dev store before testing.

Step 4: import, then re-test on launch day

Import redirects.csv under Online Store → Navigation → URL Redirects → Import. On launch day, after DNS points at Shopify, request each old URL and confirm it returns a single 301 to the expected path, not a chain. Then watch Search Console's Pages report for new 404s for the next few weeks and add redirects as they appear.

What the script doesn't cover

Redirects keep link equity flowing, but rankings also depend on the content that loads at the new address. Carry over titles and meta descriptions from Yoast or Rank Math, image alt text and product descriptions. Customer passwords can't move at all, so plan the customer communication too. If you'd rather hand the whole move to a team, Dzimark runs WooCommerce to Shopify migration projects for stores in the US, UK, Canada and Australia.

FAQ

Does Shopify let me keep /product/ URLs?
No. Product URLs always start with /products/, which is why every product needs a redirect.

Do I need an app for redirects?
No. Shopify's built-in URL Redirects page imports a CSV. Apps help mainly with bulk editing and monitoring 404s after launch.

Should I redirect URLs that get no traffic?
Redirect anything with traffic, links or that appears in Search Console. Thin, empty archives can go to the closest collection.

Can I import historical orders at the same time?
Not with a CSV. Shopify points to apps such as Matrixify or LitExtension for order history.

Further reading: the full WooCommerce to Shopify migration guide on the Dzimark blog.

Top comments (0)