On Fri, 02 May 2014 13:08:04 GMT, Luís Marques wrote:

On Fri, 13 Sep 2013 07:00:39 GMT, Sönke Ludwig wrote:

shared static this()
{
	auto redirectsettings = new HTTPServerSettings;
	redirectsettings.hostName = "example.com";
	listenHTTP(redirectsettings, &redirect);

	auto settings = new HTTPServerSettings;
	settings.hostName = "www.example.com";
	// setup a URLRouter etc. for the normal site...
	listenHTTP(settings, router);
}

void redirect(HTTPServerRequest req, HTTPServerResponse res)
{
	res.redirect("http://www.example.com" ~ req.requestURL);
}

settings and redirectsettings both use the same port and virtual host dispatch is used to route the requests appropriately. The redirectsettings.hostName field could also be left blank to handle all domains except www.example.com.

This does not preserve the original port. For instance, http://example.com:8080 is redirected to http://www.example.com.

Since this seems to be useful, boilerplate-ish and not completely trivial (corner cases like this of the port), perhaps this functionality could be packaged into vibe.d?

With today's API, the following should work for all the corner cases (at least if there are no bugs in fullURL):

void redirect(HTTPServerRequest req, HTTPServerResponse res)
{
    auto url = req.fullURL;
    url.host = "www.example.com";
    res.redirect(url);
}

A redirectSite function could be useful, but it would also have to support the case, where there is a different port/protocol/path prefix compared to the original URL, so I'm not sure how much value it would actually add.