#251: Create HTTP Redirects to the New Blog
Now that the posts for Python Friday are on the new PythonFriday.dev domain, I need a way to redirect the visitors from the Improve & Repeat blog to the new location. Let us write a script to create the necessary HTTP redirects.
Apache mod_rewrite
Since Improve & Repeat runs on Apache, we can use the mod_rewrite module. This is a feature that let us use regular expressions to match requests and redirect them with a pattern. I want to keep things simple and skip most of the dynamic part. Instead, we create a rewrite rule for each post and explicitly set the new URL. This is a tiny bit more work at the beginning, but it will be simpler to understand for everyone who has to maintain it.
Status code 302 or 301?
HTTP status code 302 is for a temporary redirect, while 301 is a durable solution. We first should check if everything works with the 302 and only after a few weeks switch to 301. That way the browser keeps checking the original location, what would help us to fix a wrong redirect.
Find the old posts
To get a list of all the old posts, we can leverage the tools we used in #139: Parse Sitemaps With Python). With the ultimate sitemap parser we can read all URLs from the sitemap, and filter the ones containing Python Friday posts:
Create the rewrite rule with a transformation
The rewrite rule follows this pattern:
It takes two substitutions to create a new URL out of an old one. We need to remove the python-friday- part and change the domain from improveandrepeat.com to pythonfriday.dev. With our old and new URL, we can create a rewrite rule and write it to a file:
Handle exceptions
For 248 out of the 249 posts we want to redirect, we can follow the rule above. Only post 186 needs a special treatment, because I missed that the Yoast plug-in changed the URL behind my back. We can put that special case into its own line at the end of our script:
Run it
The full script should now look like this:
When we run it, we get a file named redirects.txt that has all the rewrite rules in it:
Apply it in Apache
We need to log-in to the old site, open the .htaccess file for the page in an editor and paste our rules in the mod-rewrite section:
It may take a few minutes until the setting is active. You best check with a single post if the redirect works.
Verify it
With requests we can check if we get the correct redirect back from our old blog. Since the redirect happens automatically, we need to check the request history to see if it was a redirect or not:
For my server I had to slow it down a bit to prevent errors from Cloudflare. If you run the script, you should get a line like this for all posts:
Fix the URL
For some pages we may get a redirect but end up on a non-existing page. That happens if the title falls into the part of the slug generator in which MkDocs and WordPress differ. We could change the slug function or only fix the slug for the few posts that end up with the wrong URL. I prefer to add a slug meta tag in the header section of the Markdown file:
What about Cloudflare?
Should you use Cloudflare on your old site, you need to purge the cache so that Cloudflare no longer sends out the old posts. Everything else should work right away.
Next
With the redirects in place, we can look at some common mistakes with MkDocs and how we can prevent them.