Last active
March 7, 2023 20:31
-
-
Save nabilfreeman/c0dec56f149fcfb4a8e704ae672b204e to your computer and use it in GitHub Desktop.
Revisions
-
nabilfreeman revised this gist
Nov 20, 2022 . 1 changed file with 17 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 //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(request.uri.match(regex)){ //lowercase the url. let lowercase_url = `${request.uri.toLowerCase()}/`; if(request.querystring) { lowercase_url += `?${request.querystring}`; } //debug. 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(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 ${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 = request.uri.slice(-1); //if there is already a trailing slash, return. if(last_character === "/"){ console.log(`Skipping ${request.uri} because it already has a trailing slash.`); return callback(null, request); } //add a trailing slash. let trailing_slash_url = `${request.uri}/`; if(request.querystring) { trailing_slash_url += `?${request.querystring}`; } //debug. console.log(`Rewriting ${request.uri} to ${trailing_slash_url}...`); //create HTTP redirect... return callback(null, redirect(trailing_slash_url)); -
nabilfreeman revised this gist
Nov 20, 2022 . 1 changed file with 34 additions and 15 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 trailing_slash_url = `//${request.host}/${request.uri}/`; //debug. console.log(`Rewriting ${url} to ${trailing_slash_url}...`); //create HTTP redirect... return callback(null, redirect(trailing_slash_url)); }; -
nabilfreeman revised this gist
Nov 16, 2022 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 = `//${request.host}/${request.uri}/`; //debug. console.log(`Rewriting ${url} to ${new_url}...`); -
nabilfreeman created this gist
Nov 20, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); };