Skip to content

Instantly share code, notes, and snippets.

@CraigChamberlain
Last active February 9, 2024 10:04
Show Gist options
  • Save CraigChamberlain/41ec4343d4fb417e082ecac72fea2de2 to your computer and use it in GitHub Desktop.
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.
$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