Skip to content

Commit

Permalink
Add redirect from www to bare domain
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-mosher committed Sep 27, 2024
1 parent 663a58e commit 3ff4843
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"plugin:tailwindcss/recommended"
],
"ignorePatterns": [
"cloudfront-functions/*",
".eslintrc.cjs",
"dist",
"tailwind.config.ts"
Expand Down
25 changes: 25 additions & 0 deletions cloudfront-functions/handler-on-viewer-request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// AWS runtime: 'cloudfront-js-2.0'
// This function is intended to run as part of the 'Viewer Request' event in CloudFront, where it
// checks and redirects requests from 'www.whoisthecutest.com' to 'whoisthecutest.com'.

function handler(event) {
var request = event.request;
var headers = request.headers;

var bareDomain = 'whoisthecutest.com';

if (headers.host && headers.host.value === 'www.' + bareDomain) {
return {
statusCode: 308,
statusDescription: 'Permanent Redirect',
headers: {
// Redirects to the bare domain without preserving the path (request.uri).
// Since the site is a single-page application, only the base domain is needed for the redirect.
location: { value: 'https://' + bareDomain },
},
};
}

// If the host doesn't match 'www.whoisthecutest.com', proceed without redirection.
return request;
}

0 comments on commit 3ff4843

Please sign in to comment.