Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
85 lines (58 sloc) 2.56 KB

Connect Host Redirect

connect-redirecthost is middleware for the Express.js framework that allows redirecting multiple domains to a default one.

Build Status


$ npm install connect-redirecthost

Quick Start

Using connect-redirecthost is easy. Register it within Express.js as middleware by adding the following line into your app.js file before most calls to app.use(...):


Localhost is always excluded to make local development easier.


Redirect exceptions are supported. If, for example, your site uses a CDN on a separate domain, you can create an exception for that domain

 to: '',       // all requests not on will be redirected to
 except: ''    // except for those to

You can also specify multiple exceptions using an array.

 to: '',
 except: ['', '']

Path handling

By default, the root-relative path is preserved during the redirect. Should the root relative path need to be modified reset, the following options can be used, where the path will be set to the associated domain's value

    to: '',
    changePath: {'': '/ca', '': '/us'}

You can also specify a function to determine the new path;

    to: '',
    changePath: {
     '': function(host, url){return '/ca' + url;},
     '': function(host, url){return '/us' + url;}

Protocol overrides

By default, the current protocol is preserved when redirecting. Using the protocol option forces the redirect to a particular protocol. Typical values are http and https.

    to: '',
    protocol: 'https'

IMPORTANT! Keep in mind this only sets the protocol when redirecting based on host matches. This will not normalize the protocol on all requests. You'll need another module to accomplish that.


MIT License, see the LICENSE file.

Something went wrong with that request. Please try again.