Skip to content

Instantly share code, notes, and snippets.

@nabilfreeman
Last active March 7, 2023 20:31
Show Gist options
  • Select an option

  • Save nabilfreeman/c0dec56f149fcfb4a8e704ae672b204e to your computer and use it in GitHub Desktop.

Select an option

Save nabilfreeman/c0dec56f149fcfb4a8e704ae672b204e to your computer and use it in GitHub Desktop.

Revisions

  1. nabilfreeman revised this gist Nov 20, 2022. 1 changed file with 17 additions and 11 deletions.
    28 changes: 17 additions & 11 deletions lambda-redirect-to-trailing-slash.js
    Original file line number Diff line number Diff line change
    @@ -19,48 +19,54 @@ exports.handler = (event, context, callback) => {
    //get request object
    const { request } = event.Records[0].cf

    const url = request.uri;

    //if this url has uppercase letters in it, we need to direct to the all-lowercase version.
    const regex = /[A-Z]/g;

    //if we get a truthy result from the regex match, we need to 301 to the lowercase url.
    if(url.match(regex)){
    if(request.uri.match(regex)){
    //lowercase the url.
    const lowercase_url = url.toLowerCase();
    let lowercase_url = `${request.uri.toLowerCase()}/`;

    if(request.querystring) {
    lowercase_url += `?${request.querystring}`;
    }

    //debug.
    console.log(`Rewriting ${url} to ${lowercase_url}...`);
    console.log(`Rewriting ${request.uri} to ${lowercase_url}...`);

    //create HTTP redirect...
    return callback(null, redirect(lowercase_url));
    }

    //we need to determine if this request has an extension.
    const extension = path.extname(url);
    const extension = path.extname(request.uri);

    //path.extname returns an empty string when there's no extension.
    //if there is an extension on this request, continue without doing anything!
    if(extension && extension.length > 0){
    console.log(`Skipping ${url} because it has a file extension.`);
    console.log(`Skipping ${request.uri} because it has a file extension.`);
    return callback(null, request);
    }

    //there is no extension, so that means we want to add a trailing slash to the url.
    //let's check if the last character is a slash.
    const last_character = url.slice(-1);
    const last_character = request.uri.slice(-1);

    //if there is already a trailing slash, return.
    if(last_character === "/"){
    console.log(`Skipping ${url} because it already has a trailing slash.`);
    console.log(`Skipping ${request.uri} because it already has a trailing slash.`);
    return callback(null, request);
    }

    //add a trailing slash.
    const trailing_slash_url = `//${request.host}/${request.uri}/`;
    let trailing_slash_url = `${request.uri}/`;

    if(request.querystring) {
    trailing_slash_url += `?${request.querystring}`;
    }

    //debug.
    console.log(`Rewriting ${url} to ${trailing_slash_url}...`);
    console.log(`Rewriting ${request.uri} to ${trailing_slash_url}...`);

    //create HTTP redirect...
    return callback(null, redirect(trailing_slash_url));
  2. nabilfreeman revised this gist Nov 20, 2022. 1 changed file with 34 additions and 15 deletions.
    49 changes: 34 additions & 15 deletions lambda-redirect-to-trailing-slash.js
    Original file line number Diff line number Diff line change
    @@ -2,18 +2,47 @@

    const path = require('path')

    const redirect = new_url => {
    return {
    status: '301',
    statusDescription: 'Moved Permanently',
    headers: {
    location: [{
    key: 'Location',
    value: new_url,
    }],
    },
    };
    };

    exports.handler = (event, context, callback) => {
    //get request object
    const { request } = event.Records[0].cf

    const url = request.uri;

    //if this url has uppercase letters in it, we need to direct to the all-lowercase version.
    const regex = /[A-Z]/g;

    //if we get a truthy result from the regex match, we need to 301 to the lowercase url.
    if(url.match(regex)){
    //lowercase the url.
    const lowercase_url = url.toLowerCase();

    //debug.
    console.log(`Rewriting ${url} to ${lowercase_url}...`);

    //create HTTP redirect...
    return callback(null, redirect(lowercase_url));
    }

    //we need to determine if this request has an extension.
    const extension = path.extname(url);

    //path.extname returns an empty string when there's no extension.
    //if there is an extension on this request, continue without doing anything!
    if(extension && extension.length > 0){
    console.log(`Skipping ${url} because it has a file extension.`);
    return callback(null, request);
    }

    @@ -23,26 +52,16 @@ exports.handler = (event, context, callback) => {

    //if there is already a trailing slash, return.
    if(last_character === "/"){
    console.log(`Skipping ${url} because it already has a trailing slash.`);
    return callback(null, request);
    }

    //add a trailing slash.
    const new_url = `//${request.host}/${request.uri}/`;
    const trailing_slash_url = `//${request.host}/${request.uri}/`;

    //debug.
    console.log(`Rewriting ${url} to ${new_url}...`);
    console.log(`Rewriting ${url} to ${trailing_slash_url}...`);

    //create HTTP redirect...
    const redirect = {
    status: '301',
    statusDescription: 'Moved Permanently',
    headers: {
    location: [{
    key: 'Location',
    value: new_url,
    }],
    },
    };

    return callback(null, redirect);
    };
    return callback(null, redirect(trailing_slash_url));
    };
  3. nabilfreeman revised this gist Nov 16, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion lambda-redirect-to-trailing-slash.js
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@ exports.handler = (event, context, callback) => {
    }

    //add a trailing slash.
    const new_url = `${url}/`;
    const new_url = `//${request.host}/${request.uri}/`;

    //debug.
    console.log(`Rewriting ${url} to ${new_url}...`);
  4. nabilfreeman created this gist Nov 20, 2017.
    48 changes: 48 additions & 0 deletions lambda-redirect-to-trailing-slash.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    'use strict';

    const path = require('path')

    exports.handler = (event, context, callback) => {
    //get request object
    const { request } = event.Records[0].cf

    const url = request.uri;

    //we need to determine if this request has an extension.
    const extension = path.extname(url);

    //path.extname returns an empty string when there's no extension.
    //if there is an extension on this request, continue without doing anything!
    if(extension && extension.length > 0){
    return callback(null, request);
    }

    //there is no extension, so that means we want to add a trailing slash to the url.
    //let's check if the last character is a slash.
    const last_character = url.slice(-1);

    //if there is already a trailing slash, return.
    if(last_character === "/"){
    return callback(null, request);
    }

    //add a trailing slash.
    const new_url = `${url}/`;

    //debug.
    console.log(`Rewriting ${url} to ${new_url}...`);

    //create HTTP redirect...
    const redirect = {
    status: '301',
    statusDescription: 'Moved Permanently',
    headers: {
    location: [{
    key: 'Location',
    value: new_url,
    }],
    },
    };

    return callback(null, redirect);
    };