Code Monkey home page Code Monkey logo

Comments (1)

renzit avatar renzit commented on August 13, 2024

I ended with this code for all my needs:

function handler(event) {
  var shouldRedirect = false;
  var request = event.request;
  var queryString = queryStringObjectToString(request.querystring);
  var uri = request.uri;
  var regex = /[A-Z]/g;
  var headers = request.headers;
  var host = headers.host.value;
  var last_uri_character = uri.slice(-1);

  // If the URI have a dot we assume is a file and we don't redirect or rewrite
  if (uri.includes(".")) {
    return request;
  }

  // If the URI contains a capital letter, rewrite to the lowercase version
  if (regex.test(uri)) {
    uri = request.uri.toLowerCase();
    request.uri = uri;
  }

  // If the host is www we redirect to the naked domain
  if (host.startsWith("www.")) {
    host = host.replace("www.", "");
    delete headers.host;
    shouldRedirect = true;
  }

  // If the URI ends with a slash, redirect to the non-slash version
  if (last_uri_character !== "/") {
    uri = uri + "/";
    shouldRedirect = true;
  }

  // If we should redirect, do it
  if (shouldRedirect) {
    var url = "https://" + host + uri + queryString;
    var response = {
      statusCode: 301,
      statusDescription: "Permanently Moved",
      headers: { location: { value: url } },
      cookies: request.cookies,
    };
    return response;
  }
  return request;
}

/**
 * @description: Converts an event.request.querystring object back to a query string
 * @date 2022-11-28
 * @param {object} queryStringObject: Query string object as provided by event.request
 * @return {string}:                  A normalised query string in the form ?key1=valn&key2=val2...
 */
function queryStringObjectToString(queryStringObject) {
  // Convert the query string object to an array of entries and reduce to a single string
  return (
    Object.entries(queryStringObject)
      .reduce((p, q) => {
        if (!q[1].multiValue) {
          // Process a single key/value property
          return (p += q[0] + "=" + q[1].value + "&");
        } else {
          // Process multiValue properties. E.g arrays
          return (p +=
            q[1].multiValue.map((v) => q[0] + "=" + v.value).join("&") + "&");
        }
      }, "?")
      // Remove the trailing ampersand
      .slice(0, -1)
  );
}

Thanks.

from amazon-cloudfront-functions.

Related Issues (20)

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.