Virtual Hosting
Runs on your machineHosts multiple websites, one per domain, on a single server.
This sample runs on your machine only. It needs domains of its own - browsers resolve blog.localhost and shop.localhost to your machine.
Clone GenHTTP, let Playground/Program.cs serve VirtualHostingSample.Create() and start it with dotnet run - then try the commands below.
using GenHTTP.Api.Content;
using GenHTTP.Api.Protocol;
using GenHTTP.Modules.IO;
using GenHTTP.Modules.Redirects;
using GenHTTP.Modules.VirtualHosting;
public static class VirtualHostingSample
{
public static IHandlerBuilder Create()
{
/*
*
* Hosts multiple websites on a single server, one per domain. Browsers
* resolve *.localhost to your machine, so you can try this locally:
*
* http://blog.localhost:8080/
* http://shop.localhost:8080/
* http://localhost:8080/ (redirects to the blog)
*
* See https://genhttp.org/documentation/content/handlers/virtual-hosts/
*
*/
var blog = Content.From(Resource.FromString("<h1>My Blog</h1>").Type(ContentType.TextHtml));
var shop = Content.From(Resource.FromString("<h1>My Shop</h1>").Type(ContentType.TextHtml));
return VirtualHosts.Create()
.Add("blog.localhost", blog)
.Add("shop.localhost", shop)
.Default(Redirect.To("http://blog.localhost:8080/", temporary: true)); // any other host
}
}Try it
With the sample running on your machine:
Open the blog
Open the shop
Choose the site via the Host header
curl -H "Host: shop.localhost" http://localhost:8080/