Last active
February 9, 2024 10:04
-
-
Save CraigChamberlain/41ec4343d4fb417e082ecac72fea2de2 to your computer and use it in GitHub Desktop.
Make a canonical copy of your GitHub pages site at another domain or subdomain.
This file contains hidden or 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
$newDomain = "yourdomain.com" | |
$outputDir = "_redirects" | |
function redirectPage($uri) {@" | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Redirecting to https://$newDomain$uri</title> | |
<meta http-equiv="refresh" content="0; URL=https://$newDomain$uri"> | |
</head> | |
<link rel="canonical" href="https://$newDomain$uri"> | |
</html> | |
"@} | |
$pages = | |
Get-ChildItem ./_site -Recurse -File | | |
? {$_.Extension -eq ".html"} | | |
% { $_.FullName -replace '.*_site', "" | |
} | |
$dirs = | |
Get-ChildItem ./_site -Recurse -Directory | | |
% { $_.FullName -replace '.*_site', "" } | |
function Get-Uri ($filename) { | |
$filename -replace "\\","/" ` | |
-replace "\/index.html","" ` | |
-replace ".html","" | |
} | |
foreach($dir in $dirs){ | |
mkdir "$outputDir/$dir" | |
} | |
foreach($page in $pages){ | |
$uri = Get-Uri $page | |
redirectPage($uri) | Out-File $outputDir$page | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment