/apache-rewrite-rules-tool

A simple Rewrite Rules tool written in Python for Apache

Primary LanguagePythonMIT LicenseMIT


Logo

SIMPLE REDIRECT TOOL GENERATOR FOR APACHE

What is this script for?

This is a simple script in Python that will take the first two columns of an Excel file, extract its contents and use the text inside to generate a new rewriter-rules.txt file with rewrite rules for Apache.

An example of the result of each rows inside the rewrited-rules.txt is the following:

# SCTASK1234567 --- https://www.test.co.uk/en/some/old/path.html => https://www.test.co.uk/en/a/new/path.html
RewriteRule ^/en/some/old/path.html$ https://%{SERVER_NAME}/en/a/new/path.html? [NC,L,R=301,ENV=REDIRECTCACHE:1]

Where the first line is a comment that shows:

The second link is created this way:

  • RewriteRule: it's the beginning of the RewriteRule
  • ^/en/some/old/path.html: it's the old path starting from the language from the old URL
  • https://%{SERVER_NAME}/en/a/new/path.html: it's the page where the user will be redirected from the old path
  • [NC,L,R=301,ENV=REDIRECTCACHE:1]: those are redirect rules flags
    • NC: thise flag causes the RewriteRule to be matched in a case-insensitive manner
    • L: this flag, in most contexts, means that if the rule matches, no further rules will be processed.
    • R=301: it's the redirect type, 301 or 302 (choose wisely)
    • ENV=REDIRECTCACHE:1: With the [E], or [env] flag, you can set the value of an environment variable (in this case REDIRECTCACHE with value 1).
Official documentation about flags: https://httpd.apache.org/docs/2.4/rewrite/flags.html

SETTING UP VARIABLES

Open the script with an editor like Notepad++ and edit the following variables as you prefer:

  • startFromWhichRow = 1 # in case the first row of your Excel file has no rules but only column titles put 1, otherwise 0
  • sheetNumber = 0 # the index of the excel sheet you want to process, if doOnlyOneSheet is False it will do them all starting from this index
  • doOnlyOneSheet = False # if for any reason you need to do one file sheet at a time put this to True (with capital T letter)
  • serviceTaskID = "SCTASK1234567" # number of the Jira Service Task that is inserted in the line of the comments (if neededm otherwise leave it blank)
  • redirectType = "301" # can be 301 or 302, your choice
  • rewriteRulesFlags = ["NC", "L", "R="+redirectType, "ENV=REDIRECTCACHE:1"] # add all the redirect flags you need into this arrays of strings

N.B. REMEMBER TO CHANGE ADD CORRECT PATHS TO THE FILES THAT YOU'LL READ AND WRITE!

INSTALLATION GUIDE

To make this script work you need to install Python and xlrd library.

You can download the latest version of Python from those links for Windows, Linux/UNIX, MacOS.

If you are using .xlsx files, you'll need to install this specific version:

pip install xlrd==1.2.0

Otherwise, if you are using .xls files, then, you'll need to install the latest version:

pip install xlrd

Once done, navigate into your folder with cmd/terminal and launch (N.B. use your python version into the next command):

python redirect-tool.py

Now, you will find your new/updated file rewritedRules.txt into your folder destination (specified inside the script).