Skip to content

dmoruzzi/Py-HTML-Redirects

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

Py-HTML-Redirects

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.

Usage

  1. Update redirects.json
  2. python3 main.py
  3. Upload /dist to your web server
  4. Store master.json somewhere safe

Redirects.json

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

Redirects.json Limitation

  1. You must use "redirects.json" without capitalization
  2. All relative permalinks (object keys) will become lowercase
  3. Beginning slashes may introduce errors depending on operating system & master.json usage; be consistent.

Notes

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.

Miscellaneous Notes

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.

About

Lightweight Python script for HTML redirects

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages