March 29, 2024

Generate HTML redirects automatically with Python Script

Anyone who migrates a website or plans a relaunch faces the challenge of not losing valuable traffic immediately after the relaunch. The traffic comes from search engines that have indexed the website and particularly the subpages, but also via backlinks, which usually also link to subpages of the website.

Missing redirects cause valuable traffic and visitor numbers to collapse

Shortly after a relaunch or website migration, the subpages are named differently and the link looks different than before. On large websites, many hundreds of individual pages can be affected.

Search engines like Google link to the old pages before the new indexing and searchers end up in nowhere or on an error 404 page. Backlinks or links in social networks that were created in the past also link to the old page names.

How do redirects work?

With a simple HTML code you can ensure that visitors are automatically redirected through their browser. For this purpose, HTML offers the meta tag with the attribute http-equiv="refresh", which is supported by most modern browsers. The trick is to keep all old links available and create a redirect to the new links there. Of course, this requires appropriate planning for a website migration or a relaunch, and the old and new links should be kept in a list.

How to (easily) create redirects?

If only a few pages are affected, then take a look at our Redirect Generator, which generates the HTML code for a page for the redirect.

If multiple pages are affected the following Python Script may help you:

import csv
 
with open('redirects.csv', mode ='r')as file:
   
  csvFile = csv.reader(file)
 
  for lines in csvFile:
        with open(lines[0], 'w') as f:
            f.write('<html><head><meta http-equiv="refresh" content="0; URL='+lines[1]+'"></head><body><h1>Page does not exist, you will be redirected.</h1><p>If your browser does not support automatic redirection, <a href="'+lines[1]+'" >click here</a>!</p></body></html>')

The input for this script is the redirects.csv in which the old links and the new links are listed. Example:

home-en.php,/
company-information.php,/home/company-info/
terms-of-use.php,/home/company-info/privacy-policy/
posting-guidelines.php,/home/company-info/posting-guidelines/
partners.php,/home/partners/
sitemap-en.php,/home/sitemap/
article-en.php,/articles/
general.php,/articles/computer-basics/

The script creates a file with the name on the left side of the CSV line. The content of the file is an HTML redirect with the target on the right. The destination can also be another domain.

These files are generated with the example CSV:

The content of the file is the corresponding HTML redirect code. For example the file sitemap-en.php:

<html><head><meta http-equiv="refresh" content="0; URL=/home/sitemap/"></head><body><h1>Page does not exist, you will be redirected.</h1><p>If your browser does not support automatic redirection, <a href="/home/sitemap/" >click here</a>!</p></body></html>

Now the files have to be placed in the right place. In the example, this website (Computer-Masters.net) has been updated and the links have changed. The PHP files were placed in the root directory of Computer-Masters.net using FileZilla, so that the links can be accessed as before the relaunch, but redirect directly to the new links.

Leave a Reply

Your email address will not be published. Required fields are marked *