learning-website-framework1-7 Exercise 3: Redirect Rules You Can Test ====================================================================== When the languages area moves to languages.osztromok.com, every old URL must redirect to the same path on the new host in one hop. Build the Apache rule from the site map and test it against every old page URL. Needs sitemap.py (Chapter 5) and sitemaps.py (Exercise 1). Save as redirects.py: import re, sys from sitemap import SITES from sitemaps import collect, by_site def build_rule(site): folders = "|".join(re.escape(f) for f in SITES[site]) pattern = rf"^/({folders})(/.*)?$" target = f"https://{site}.osztromok.com/$1$2" return pattern, target, f"RedirectMatch 301 {pattern} {target}" def apply(pattern, target, old_path): m = re.match(pattern, old_path) if not m: return None return target.replace("$1", m.group(1)).replace("$2", m.group(2) or "") if __name__ == "__main__": content = sys.argv[1] site = sys.argv[2] pages = collect(content) new_urls = {f"https://{site}.osztromok.com{u}" for u in by_site(pages)[site]} pattern, target, rule = build_rule(site) print(rule) old = [u for u in pages if re.match(pattern, u)] ok = bad = 0 examples = [] for u in sorted(old): dest = apply(pattern, target, u) if dest in new_urls: ok += 1 else: bad += 1 examples.append((u, dest)) print(f"old URLs matched by the rule: {len(old)}") print(f"redirect to a page that exists on the new site: {ok}") print(f"redirect to a page that does NOT exist: {bad}") for u, d in examples[:5]: print(f" {u} -> {d}") # a path that the rule must not touch print("rule leaves /linux/ alone:", re.match(pattern, "/linux/") is None) Run it: python redirects.py "/claude-projects/website-content/content" languages Output (checked by running it): RedirectMatch 301 ^/(france|germany|hungary|japan|culture)(/.*)?$ https://languages.osztromok.com/$1$2 old URLs matched by the rule: 594 redirect to a page that exists on the new site: 594 redirect to a page that does NOT exist: 0 rule leaves /linux/ alone: True Limits of this test (read this) ------------------------------- The check "redirects to a page that exists on the new site" is partly circular: the new URLs are computed from the same folders as the old ones, so it can only fail if the rule itself is wrong (for example a folder missing from the pattern). What it does prove is that the rule matches every old languages URL, rewrites the path correctly, and leaves other areas alone. The real test comes after the new site is built: request every old URL and check that the final page returns 200 (Learning Website: Framework & Architecture 11). Not covered here: the kanji pages, which are served from /resources/japanese/kanji/ (not a content folder name) and need their own rule. Google's site-move guidance (checked): - use permanent redirects (301 or 308) - keep them for as long as possible, generally at least a year - a section-by-section move is acceptable for a large site - a small or medium site can take a few weeks for most pages to move Use 302 while testing so browsers do not cache the redirect. WHY THIS WORKS AS AN ANSWER --------------------------- The redirect rule is built from the same map as the sitemap and the menus, so it moves with them. Testing it against the real list of old URLs catches a missing folder before visitors do, and the notes say honestly what the test cannot prove.