This is a lightweight Python script that will automatically create vast quantities of HTTP redirect pages from a JSON file. The python script will automatically load redirects.json
from the current path and output the appropriate redirects in the dist
directory. No special .htaccess, conf.d, or httpd.conf. Instead, a basic solution that will work on almost every web server, including Lighttpd, Apache HTTP Server, and Nginx.
- Update redirects.json
python3 main.py
- Upload
/dist
to your web server - Store
master.json
somewhere safe
The redirects.json
file is a simple file that expects the object key to be the relative permalink and this corresponds to the object value that is the redirect href address.
{
"msu": "https://www.msu.edu",
"msu/egr": "https://www.egr.msu.edu"
}
This will produce the following results in the dist
directory:
msu/index.html -> https://www.msu.edu
msu/egr/index.html -> https://www.egr.msu.edu
- You must use "redirects.json" without capitalization
- All relative permalinks (object keys) will become lowercase
- Beginning slashes may introduce errors depending on operating system & master.json usage; be consistent.
All relative permalinks (object keys) will become lowercase to conform to established expectations. This is an artificial limitation and may be lifted by removing .lower()
from permalink
. If multiple object keys are equivalent when in lowercase, the last referenced key (lowest key in redirects.json
) will be used.
To prevent unnecessary computation time, once successfully executed, this script will clear redirects.json
and update the master.json
file to all generated redirects.
The master.json
is a compilation of all generated redirects. If a permalink is no longer deployed, please remove the object key and value from this file, if appropriate. Likewise, this master.json should be stored for documentation, recovery, and future uses.
The master.json
is loaded each time the application executes. If working with very large datasets, this may become an issue. Renaming master.json
master assist; likewise, commenting all references to master_json_dict
and update_master_json()
would prevent this memory and disk usage.
If possible and appropriate, NGINX Rewrite Rules is much faster.
This application is written in Python3, an interpreted language. C++ or GoLang may provide better speed. However, with such a lightweight script, a noticeable difference is unlikely.