Last active
June 24, 2020 19:34
-
-
Save iMattPro/ed3af3251a65c88ad77bceee01dbd487 to your computer and use it in GitHub Desktop.
301 style htaccess redirects in Javascript for GitHub Pages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 301 style htaccess redirects in Javascript for GitHub Pages. | |
* | |
* This simple script can be used on GitHub Pages hosted sites to handle redirects you would normally have used | |
* htaccess for. In this case, a 301 redirects from an old URL path to the new URL location. Note that this does | |
* not actually send 301 headers. GitHub Pages will still send a 404 header, so this is not an SEO or search | |
* engine solution, but without access to GitHub's server config, this will at least send your visitors to the | |
* correct page when browsing. | |
* | |
* Usage: Include this script inside your GitHub Pages 404.html page. | |
*/ | |
// This object holds all of your 301 redirects. Don't include URL parameters (?foo=bar|#anchor), they're handled automatically. | |
var redirects = { | |
"/path/of/old/file.html": "https://yourdomain.com/path/to/new/file.html", | |
"/path/of/old/file.php": "https://yourdomain.com/path/to/new/file.html" | |
}; | |
// Get the URL requested in the browser | |
var url = redirects[location.pathname]; | |
// Perform the redirect | |
if (typeof url !== "undefined") location.href = url + location.search + location.hash; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment