Skip to content
Lambda@Edge function for handling redirects and rewrites
Branch: master
Clone or download
Latest commit ef63780 May 2, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Don't log undefined Jul 9, 2018
tests
.gitattributes
.gitignore Add unit tests, use yaml, support rewrites May 21, 2018
.travis.yml Troubleshoot travis deploy script syntax Jul 18, 2018
README.md docs: Add examples, reorganise readme Jul 30, 2018
clauda.json
claudia.json
deploy.sh Deploy script should use non-zero exit code if missing args Jul 18, 2018
package-lock.json
package.json
rules.txt Added rules/inclusive-growth May 2, 2019

README.md

phila.gov router Build Status

Lambda@Edge function for handling redirects and rewrites

usage

Store your redirect and rewrite rules in rules.txt.

/otis                     301 http://www.phillyotis.com
/parksandrec/(.*)         301 /departments/philadelphia-parks-recreation/
/revenue/(.*)             301 /departments/department-of-revenue/$1
/contracts/data$          301 /contracts/data/
/contracts/data/(.*)      200 https://cityofphiladelphia.github.io/contracts/$1

Use status code 301 for a permanent redirect, and 200 for a rewrite.

Patterns are converted to regexes with the following enhancements:

  • Case-insensitive
  • ^ is prepended (unless it already starts with ^)
  • /?$ is appended (unless it already ends with $)

examples

Redirect /otis or /otis/ to their external site:

/otis  301  http://phillyotis.com

Redirect /eeocomplaint and /eeocomplaints and /eeo-complaints to its service page:

/eeo-?complaints?  301  /services/working-jobs/file-a-sexual-harassment-complaint/

Redirect anything under /parksandrec to the new homepage:

/parksandrec/?.*  301  /departments/parks-and-recreation/

Redirect anything under /oem to its expanded route:

/oem/?(.*)  301  /departments/office-of-emergency-management/$1

Rewrite /contracts/data to mask a site on github pages (note the redirect for a missing trailing slash, which is otherwise handled by S3's static file hosting feature):

/contracts/data$      301  /contracts/data/
/contracts/data/(.*)  200  https://cityofphiladelphia.github.io/contracts/$1

tips

  • You can use sites like regex101 to test your regular expressions.
  • You can use tsv-pretty to format the file to be more readable.

deployment

Deployment will happen automatically via Travis CI based on the configuration in .travis.yml (assuming tests pass):

  • Pushes to the staging branch will deploy to the staging environment.
  • Pushes to the master branch will deploy to the production environment.

You can skip deployment on a push by specifying [no ci] in the commit message.

local development

install node.js dependencies

npm install

run tests

npm test

manual deployment

$ ./deploy.sh $LAMBDA_NAME $LAMBDA_ROLE $S3_BUCKET $CLOUDFRONT_DISTRIBUTION_ID
You can’t perform that action at this time.