Skip to content
This repository has been archived by the owner on Oct 21, 2022. It is now read-only.

Super simple Node app that redirects all requests for an old Heroku app to a new URL

License

Notifications You must be signed in to change notification settings

watermarkchurch/heroku-redirect

 
 

Repository files navigation

Deploy to Heroku

This app redirects all requests to new domains based on a set of rules configured in environment variables. You can have as many rules as you want. Rules are processed in lexical order, the first matching rule is used.

Configuration

To redirect all requests to www.example.com:

$ heroku config:add RULE_1="* https://www.example.com"

$ curl -I https://heroku-redirect.herokuapp.com/test
HTTP/1.1 302 Found
Server: Cowboy
Connection: keep-alive
X-Powered-By: Express
Location: https://www.example.com

To use an alternative HTTP status code, add it to the rule. The default is 302 temporary redirect.

$ heroku config:add RULE_1="* https://www.example.com 301"

$ curl -I https://heroku-redirect.herokuapp.com/test
HTTP/1.1 301 Moved Permanently
Server: Cowboy
Connection: keep-alive
X-Powered-By: Express
Location: https://www.example.com

To add a query parameter to the redirected location, simply set it in the destination url

$ heroku config:add RULE_1="https?//ex.com https://www.example.com?utm_source=ex.com 301"

$ curl -I https://ex.com
HTTP/1.1 301 Moved Permanently
Server: Cowboy
Connection: keep-alive
X-Powered-By: Express
Location: https://www.example.com?utm_source=ex.com

To preserve the path and query from the request, set the "preserve" option

$ heroku config:add RULE_1="* https://www.example.com?utm_source=ex.com 302 preserve"

$ curl -I https://heroku-redirect.herokuapp.com/test?q=something
HTTP/1.1 302 Found
Server: Cowboy
Connection: keep-alive
X-Powered-By: Express
Location: https://www.example.com/test?utm_source=ex.com&q=something

To differentiate by host (or even by path criteria) set multiple rules.

$ heroku config:add RULE_1="https://www.my-old-site.com https://www.my-new-site.com/legacy 301 preserve"
$ heroku config:add RULE_999="* https://www.example.com"

$ curl -I https://heroku-redirect.herokuapp.com/test
HTTP/1.1 301 Moved Permanently
Server: Cowboy
Connection: keep-alive
X-Powered-By: Express
Location: https://www.my-new-site.com/legacy/test

$ curl -I https://heroku-redirect.herokuapp.com/test
HTTP/1.1 302 Found
Server: Cowboy
Connection: keep-alive
X-Powered-By: Express
Location: https://www.example.com

Matching is done using the path-to-regexp library. The first matching rule in numerical order is used, i.e. RULE_9 has precedence over RULE_10.

About

Super simple Node app that redirects all requests for an old Heroku app to a new URL

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 62.4%
  • TypeScript 37.5%
  • Procfile 0.1%