Giter VIP home page Giter VIP logo

connect-redirecthost's Introduction

Connect Host Redirect

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

Build Status

Installation

$ 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(...):

app.use(require('connect-redirecthost').redirectHost('www.example.com'));

Localhost is always excluded to make local development easier.

Exceptions

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

app.use(require('connect-redirecthost').redirectHost({
 to: 'www.example.com',       // all requests not on www.example.com will be redirected to www.example.com
 except: 'cdn.example.com'    // except for those to cdn.example.com
}));

You can also specify multiple exceptions using an array.

app.use(require('connect-redirecthost').redirectHost({
 to: 'www.example.com',
 except: ['cdn.example.com', 'origin.example.com']
}));

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

app.use(require('connect-redirecthost').redirectHost({
    to: 'www.example.com',
    changePath: {'www.example.ca': '/ca', 'www.example.us': '/us'}
}));

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

app.use(require('connect-redirecthost').redirectHost({
    to: 'www.example.com',
    changePath: {
     'www.example.ca': function(host, url){return '/ca' + url;},
     'www.example.us': 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.

app.use(require('connect-redirecthost').redirectHost({
    to: 'www.example.com',
    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.

License

MIT License, see the LICENSE file.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.