12

I am serving a static directory like so:

var app = express.createServer();
app.configure(function(){
    app.use(express.static(__dirname + '/public'));
});

So I am not using routes at all. I would like to redirect example.com to www.example.com, is this possible using Express?

Tom
  • 927
  • 3
  • 12
  • 20
  • While you can certainly do this at the application layer with Express, in most situations you would want to let your web server handle this. That will help keep the load off your application when it doesn't need to be there. – Brad Jan 20 '15 at 04:46
  • Also see http://stackoverflow.com/questions/7013098/node-js-www-non-www-redirection – James Gentes Sep 21 '15 at 15:13
  • Hope this https://stackoverflow.com/a/39731460/1946016 worked for you, If so please accept the answer – karthikdivi Nov 09 '17 at 09:57

4 Answers4

11

Yes. This should do it.

var express = require("express");
var app = express.createServer();
var port = 9090;
app.all(/.*/, function(req, res, next) {
  var host = req.header("host");
  if (host.match(/^www\..*/i)) {
    next();
  } else {
    res.redirect(301, "http://www." + host);
  }
});
app.use(express.static(__dirname + "/public"));
app.listen(port);
Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
  • don't you need to redirect to port 9090 too? – chovy Oct 05 '12 at 08:02
  • you need to reverse your res.redirect args. – chovy Oct 05 '12 at 08:18
  • For express 2.x it was `res.redirect = function(url, status){`. It has changed in 3.x to swap the order. I'll update the example. And typically there is a web server listening on port 80 and reverse proxying to express on a different port (9090 in this example), so you don't want to include the 9090 port in your URL. If express was serving directly to the internet, then yes you would need the port if you weren't using the default ports. – Peter Lyons Oct 05 '12 at 14:14
10

The following code preserve path while redirecting

Ex: http://foo.com/foo to http://www.foo.com/foo

    var app = express.createServer();
    self.app.all(/.*/, function(req, res, next) {
      var host = req.header("host");
      if (host.match(/^www\..*/i)) {
        next();
      } else {
        res.redirect(301, "http://www." + host + req.url);
      }
    });
    app.use('/',express.static('public'));
karthikdivi
  • 3,466
  • 5
  • 27
  • 46
9

Alternatively, you can use a ready-made module for Express that does exactly what you want, e.g. node-force-domain.

See https://github.com/goloroden/node-force-domain for details.

Golo Roden
  • 140,679
  • 96
  • 298
  • 425
  • Let's continue discussing this on GitHub, I have seen that you opened [an issue](https://github.com/goloroden/node-force-domain/issues/1) for that :-) – Golo Roden Oct 05 '12 at 13:56
  • 1
    Thanks for this module Golo. To me Google analytics was frequently complaining about "Reduntdant Hostnames - To avoid this problem, consider setting up a 301 redirect from one of your redundant hostnames to the other, or create a search-and-replace filter that strips "www." from hostnames.". Your module perfectly solves it. {{moderators google analytics message added intentionally here, it will help fellow coders to find this thread easily}} – lame_coder Apr 18 '15 at 02:25
  • It seems like this works for the root of the website but not for other urls. Is this the expected bahavior or am I doing something wrong? – Abramodj May 01 '20 at 09:01
3

You can use express-force-domain package from npm:

//install
npm install express-force-domain

///app.js
app.all('*', require('./express-force-domain')('http://www.example.com') );

Package on npm: https://npmjs.org/package/express-force-domain

chovy
  • 72,281
  • 52
  • 227
  • 295