-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve support for and documentation of adding a custom domain name …
…and managed cert
- Loading branch information
Showing
7 changed files
with
82 additions
and
23 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
export function useRedirect(app) { | ||
const baseUrl = process.env.BASE_URL | ||
let baseHostname = "" | ||
let baseProtocol = "" | ||
|
||
if (baseUrl) { | ||
const url = new URL(baseUrl) | ||
baseHostname = url.hostname | ||
baseProtocol = url.protocol.replace(":", "") | ||
} | ||
|
||
if (baseHostname) { | ||
// Redirect to the canonical hostname | ||
app.use((req, res, next) => { | ||
// The `x-azure-ref` is set when the requst comes from the CDN | ||
// https://learn.microsoft.com/en-us/azure/frontdoor/front-door-http-headers-protocol#from-the-front-door-to-the-backend | ||
if (req.hostname !== baseHostname && !req.headers["x-azure-ref"]) { | ||
res.redirect(308, `${baseProtocol}://${baseHostname}${req.originalUrl}`) | ||
return | ||
} | ||
|
||
next() | ||
}) | ||
} | ||
|
||
if (baseProtocol === "https") { | ||
// Ensure HTTPS only | ||
app.use((req, res, next) => { | ||
if (req.protocol === "http") { | ||
res.redirect(`https://${req.hostname}${req.originalUrl}`) | ||
return | ||
} | ||
|
||
next() | ||
}) | ||
} | ||
|
||
// No trailing slashes on GET requests | ||
app.get("*", (req, res, next) => { | ||
if (req.path.endsWith("/") && req.path.length > 1) { | ||
const query = req.url.slice(req.path.length) | ||
const safepath = req.path.slice(0, -1).replace(/\/+/g, "/") | ||
|
||
res.redirect(301, safepath + query) | ||
return | ||
} | ||
|
||
next() | ||
}) | ||
} |