Giter VIP home page Giter VIP logo

standard-redirects-for-cloudfront's Introduction

standard-redirects-for-cloudfront

A Lambda@Edge function that implements standard web server redirects that simplify directory handling.

For example, requests for URI paths that end in "/" are rewritten into "/index.html" before the request is passed on to the CloudFront Origin. You may think of that as an "internal" redirect in webserver terms.

URI paths that end in ".../index.html" are redirected to ".../" with an HTTP status code 301 (Moved Permanently). This is the same as an "external" redirect by a webserver.

URI paths that do not have an extension and do not end with a "/" are redirected to the same path with an appended "/" with an HTTP status code 301 (Moved Permanently). This is again an "external" redirect. (This may hide actual content from the Origin, if you use paths without extensions!).

Examples

/foo/bar/ -> internal redirect -> /foo/bar/index.html /foo -> external redirect (301) -> /foo/ -> internal redirect -> /foo/index.html /foo.html -> no redirect /foo/bar.html -> no redirect /foo/index.html -> external redirect (301) -> /foo/

Notes

This URL scheme is somewhat opinionated. It tries to balance SEO requirements with server-side tooling. (E.g. S3 tooling tries to infer the content-type from the file extension.)

It allows you to have very nice outward facing URLs like "/cooltopic", that internally use a file with a correct extension: "cooltopic/index.html". To have content other than index.html in a folder, you need to expose the file extension: "/cooltopic/somecontent.html"

Make sure that your CloudFront distribution handles the URL "/" directly by having the property "Default Root Object" set to "index.html".

Updating

Lambda Runtime

This package uses the nodejs10.x Node.js 10 Runtime. Updating the package in your AWS account requires one manual step, which is outlined below.

Procedure

  1. Install the application "standard-redirects-for-cloudfront".
  2. Go to the Cloudformation Console
  3. Select the Output Value, this is the ARN (including the version) for the Lambda function.
  4. In CloudFront edit a Behaviour and add a Lambda Function Association with the Event Type "Origin Request" and enter the Lambda function ARN from the previous step.
  5. Wait for the CloudFront distribution to deploy.

Installation

Installation via the Serverless Application Repository

  1. Install the application "standard-redirects-for-cloudfront".
  2. Go to the Cloudformation Console
  3. Select the created role (which brings you to the IAM console)
  4. edit the trust relationship, set the policy to:
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": [
          "lambda.amazonaws.com",
          "edgelambda.amazonaws.com"
        ]
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

(This allows CloudFront to execute this function as a Lambda@Edge function.)

  1. Select the Output Value, this is the ARN (including the version) for the Lambda function.
  2. In CloudFront edit a Behaviour and add a Lambda Function Association with the Event Type "Origin Request" and enter the Lambda function ARN from the previous step.
  3. Wait for the CloudFront distribution to deploy.

Contact

For any questions or comments, do contact us!

Email: [email protected] Web: https://www.digital-sailors.de/

Enjoy!

standard-redirects-for-cloudfront's People

Contributors

digitalkapitaen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.