{"id":20278739,"url":"https://github.com/zrierc/cloudfront-subdomain-redirection","last_synced_at":"2025-03-04T01:46:22.329Z","repository":{"id":173938922,"uuid":"651512048","full_name":"zrierc/cloudfront-subdomain-redirection","owner":"zrierc","description":"CloudFront Functions for redirect from subdomain to main domain with specific path.","archived":false,"fork":false,"pushed_at":"2023-06-19T09:10:38.000Z","size":8,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-14T07:09:43.840Z","etag":null,"topics":["aws-lambda","cloudfront","cloudfront-functions"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zrierc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-06-09T11:59:50.000Z","updated_at":"2023-06-09T12:08:53.000Z","dependencies_parsed_at":null,"dependency_job_id":"fc00c1b0-49e1-41a3-b1f0-9808348c1c90","html_url":"https://github.com/zrierc/cloudfront-subdomain-redirection","commit_stats":null,"previous_names":["zrierc/cloudfront-subdomain-redirection"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zrierc%2Fcloudfront-subdomain-redirection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zrierc%2Fcloudfront-subdomain-redirection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zrierc%2Fcloudfront-subdomain-redirection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zrierc%2Fcloudfront-subdomain-redirection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zrierc","download_url":"https://codeload.github.com/zrierc/cloudfront-subdomain-redirection/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241768282,"owners_count":20017129,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws-lambda","cloudfront","cloudfront-functions"],"created_at":"2024-11-14T13:26:02.987Z","updated_at":"2025-03-04T01:46:22.299Z","avatar_url":"https://github.com/zrierc.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CloudFront Subdomain Redirection\n\n## Overview\n\nThis is CloudFront Function that maps subdomain and redirects it into a specific path. For example, if the user access `https://about.mysite.com` it will automatically redirect to `https://mysite.com/about`.\n\nHere's another example that this function support:\n\n- `contact.mysite.com -\u003e mysite.com/contact`\n- `career.mysite.com/latest -\u003e mysite.com/career/latest`\n- `skills.mysite.com/programming?sort=name\u0026foo=bar -\u003e mysite.com/skills/programming?sort=name\u0026foo=bar`\n\n## How It Works\n\n\u003e **Note**: The function associated with a CloudFront distribution with `viewer request` as the **event type**.\n\nThis function will look up at [request object](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-event-structure.html#functions-event-structure-request). Get all necesarry properties such as uri, query string, and host headers. Manipulate them into new URL and return HTTP status code `301` which will redirect to the new URL.\n\n## Setup / Installation\n\n1. Compile `index.ts` in this repository to `js` or you can copy [this `index.js`](https://github.com/zrierc/cloudfront-subdomain-redirection/blob/ready-to-use/index.js) that ready to use.\n\n2. Create CloudFront function. Paste `index.js` that you copied before in function code section.\n\n   \u003e Follow [this guide](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/create-function.html) to create CloudFront Functions.\n\n3. Associate and publish CloudFront function that you create before with the following settings:\n\n   - Distribution: \\\u003cselect distribution that you want to associate with the function\\\u003e\n\n   - Event Type: `Viewer request`\n\n     \u003e **Note** Make sure you choose `Viewer request` or the function won't work.\n\n   - Cache behavior: \\\u003cselect a cache behaviour that you use or you can use `Default (*)` instead\\\u003e\n\n     \u003e For more detail step how to associate function please visit [this guide](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/associate-function.html).\n\n## FAQ\n\n#### Can I customize the code?\n\nYes, you can. Make sure you use `ECMAScript (ES) v5.1` because when this code was written, CloudFront Functions [had many limitations](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-javascript-runtime-features.html).\n\n#### The example above only uses the root domain. Is it possible to use `www` as the main/base domain?\n\nYes. It is possible use `www` as main/base domain (for example: `www.mysite.com`). Simply change variable `baseUrl` inside `handler` function:\n\n```js\nfunction handler(event) {\n  ...\n  var baseUrl =\n    'www.' + fullDomainName.slice(fullDomainName.length - 2).join('.');\n  ...\n}\n```\n\n#### Does this support fourth level domains (e.g. `foo.bar.mysite.com`)?\n\nNo. This functions only support third level domain.\n\n#### Nooo. ES5 sucks. I only write JS using ES6 or above\n\nUse what can be used and supported.\n\n\u003cimg width=\"180\" height=\"210\" alt=\"\" src=\"https://media.tenor.com/jHvyFefhKmcAAAAd/mujikcboro-seriymujik.gif\" /\u003e\n\n## Contribution\n\nIf you have any ideas/questions/found any problem or want to improve this code let me know by opening PR/issue - your feedback and contributions are welcome!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzrierc%2Fcloudfront-subdomain-redirection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzrierc%2Fcloudfront-subdomain-redirection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzrierc%2Fcloudfront-subdomain-redirection/lists"}