Очень полезная вещь это заставить сделать Редирект если hostname или домен, по которому зашёл клиент не тот, который мы ожидали
Например, есть два или более доменов, все они указывают на один и тот же сайт, а сайт распределяет по разным папкам каждый запрос в зависимость от названия домена.
supersite.ru –> /s1/
megashop.ru –> /s2/
other.ru –> /o/
и т.д.
можно установить HttpModule на каждый сайт ASP.NET, чтобы заставить сайт использовать только предназначенный УРЛ (URL) или домен.
Код и настройки сделано на C#
...
...
source code (Код С#) модуля RedirectUrls.cs
using System;
using System.Web;
using System.Web.UI;
using System.Web.Configuration;namespace ForceRedirectModule
{
///
/// Summary description for IHttpModule
///
public class RedirectUrls : IHttpModule
{
#region IHttpModule Members
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += OnPreRequestHandlerExecute;
}
public void Dispose()
{
}
#endregion
private void OnPreRequestHandlerExecute(object sender, EventArgs e)
{
HttpContext ctx = ((HttpApplication)sender).Context;
IHttpHandler handler = ctx.Handler;
// Only worry about redirecting pages at this point
// static files might be coming from a different domain
if (handler is Page)
{
if (ctx.Request.Url.Host != WebConfigurationManager.AppSettings["ForceHostName"])
{
UriBuilder uri = new UriBuilder(ctx.Request.Url);
uri.Host = WebConfigurationManager.AppSettings["ForceHostName"];
// Perform a permanent redirect - I've generally implemented this as an
// extension method so I can use Response.PermanentRedirect(uri)
// but expanded here for obviousness:
ctx.Response.AddHeader("Location", uri.ToString());
ctx.Response.StatusCode = 301;
ctx.Response.StatusDescription = "Moved Permanently";
ctx.Response.End();
}
}
}
}
}