Skip to content

Instantly share code, notes, and snippets.

@joeyblake
Created April 29, 2014 14:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeyblake/11402110 to your computer and use it in GitHub Desktop.
Save joeyblake/11402110 to your computer and use it in GitHub Desktop.
Redirect all subdomains to main domain: ExpressJS/Ghost
/*
* redirect all subdomains example
* example domain name testsite.com
*/
// split the incoming host to get the first element
var incoming = req.headers.host.split('.')[0];
// check if it matches your domain
if ( incoming !== 'testsite' ) {
// if it doesn't match, strip the subdomain and redirect
res.redirect(301, 'http://testsite.com' + req.url);
} else {
next();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment