I read an answer about this and tried to implement it yet was stuck in an endless redirect loop.
I have a domain maxmastalerz.com and If anyone visits it from www.maxmastalerz.com I would like it to go to maxmastalerz.com (no www).
So, The method I implemented is seen in my code here:
My routes file:
var app = require('../app.js');
app.get('/*', function(req, res, next) {
    if (req.headers.host.match(/^www/) !== null ) {
        res.redirect('http://' + req.headers.host.replace(/^www\./, '') + req.url);
    } else {
        next();
    }
});
app.get('/', function(req, res) {
    res.render('index.html', {});
});
And finally, if you need it: My app.js file:
var express     = require('express');
var http        = require('http');
var path        = require('path');
var app         = module.exports = express();
var cacheTime   = 86400000*7; //a week
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.engine('html', require('hogan-express'));
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.compress());
app.use(express.static(__dirname + '/public',{ maxAge: cacheTime }));
// development only
if ('development' === app.get('env')) {
  app.use(express.errorHandler());
}
require('./mongodb.js');
require('./routes/index.js');
http.createServer(app).listen(app.get('port'), function(req,res){
    console.log('Express server listening on port ' + app.get('port'));
});
So, can anyone explain why this is happening?
P.S: This is being hosted on Heroku and the domain setting there is: www.maxmastalerz.com
This code redirects to maxmastalerz.com but endlessly loops, it may be my domain setup here:
HOST NAME                   IP ADRESS/URL                        RECORD TYPE
@                           http://www.maxmastalerz.com          URL FRAME
www                         maxmastalerz.herokuapp.com.          CNAME (Alias)
				
				In the heroku settings for you app add the following domains:
maxmastalerz.com
www.maxmastalerz.com
Next, update your dns settings to the following:
@       name.herokuapp.com            CNAME(Alias)
www     name.herokuapp.com            CNAME(Alias)
Note, I am not sure if the following settings will work with email adresses under that domain.