// the middleware function
module.exports = function() {
    return function(req, res, next) {
		var array = /(.*)\.(?:(?:.*)\.(?:.*)|localhost)/i.exec(req.host);
		if(array != null && array.length > 0)
		{
			if(array[1] != 'www'){
				var subdomains = array[1].split('.').join('/');
				req.url = '/' + subdomains + req.url;
			}
		}
    	next();
    }
    
};
@@ -50,56 +50,565 @@
 ion(
-) %7B%0A    return function(req, res, next) 
+options) %7B%0A%09//WARNING : Our regex doesn't detect second leved domains, if you have so, please specify the baseURL%0A%09//Example of second level domain : .co.uk%0A%09//For more informations please refer to http://en.wikipedia.org/wiki/Second-level_domain%0A%0A%09//Check if the baseURL option is activated%0A    var options = options %7C%7C %7B%7D;%0A%09options.baseURL = options.baseURL %7C%7C false;%0A%0A%09//Create the corresponding Regex according to options%0A%09if(options.baseURL)%0A%09%7B%0A%09%09var regex = new RegExp(%22(.*)%5C.(?:%22 + escapeRegExp(options.baseURL) + %22%7Clocalhost)%22,'i');%0A%09%7Delse%0A%09
 %7B%0A%09%09var 
 arra
@@ -603,21 +603,21 @@
 %7B%0A%09%09var 
-array
+regex
  = /(.*)
@@ -648,16 +648,123 @@
 lhost)/i
+;%0A%09%7D%0A%0A    return function(req, res, next) %7B%0A %09%09//Apply the regex and get the subdomains%0A%09%09var array = regex
 .exec(re
@@ -816,16 +816,76 @@
  0)%0A%09%09%7B%0A
+%09%09%09//If we detect the subdomain as www we don't do anything%0A
 %09%09%09if(ar
@@ -1009,30 +1009,161 @@
 %09%09%7D%0A
-    %09next();%0A    %7D%0A    
+%09%09%0A%09%09//Go to the next middleware%0A    %09next();%0A    %7D  %0A%7D;%0A%0Afunction escapeRegExp(str) %7B%0A  return str.replace(/%5B%5C-%5C%5B%5C%5D%5C/%5C%7B%5C%7D%5C(%5C)%5C*%5C+%5C?%5C.%5C%5C%5C%5E%5C$%5C%7C%5D/g, %22%5C%5C$&%22);
 %0A%7D
-;

add support of second level domains