visionmedia/express — Issue #980: Rewriting url
Author: eknkc
Created: 2012-01-27 21:01:11+00:00
State: open

Is it safe to change req.url in a middleware?
I'm trying to route all sobdomains to `/_{subdomain}/` so I would not need vhosts for seperation. I came up with this middleware module, which rewrites the req.url but I'm not use if it's a good idea.

```javascript
module.exports = function(hostname) {
if (!hostname) throw new Error('subdomain hostname required');

var regexp = new RegExp('^((.*?)\.?)' + hostname + '$', 'i');
var favico = new RegExp('/favicon.ico$', "i");

return function(req, res, next) {
if (req.headers.host) {
if (favico.test(req.url)) {
return next();
}

var host = req.headers.host.split(':')[0];
var subd = regexp.exec(host);

if (subd && subd[2] !== "www")
req.url = "/_" + subd[2] + req.url;
}

next();
};
};
```

Also, is there any other solution for this problem?