Sono andato con la seguente soluzione in passato, quando non sono stato in grado di modificare le impostazioni di IIS.
O in un HTTPModule (probabilmente più pulito) o global.asax.cs in Application_BeginRequest o in un evento di tipo BasePage, come OnInit I esegue un controllo sull'URL richiesto, con una stringa nota che desidero utilizzare:
public class SeoUrls : 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["FullHost"])
{
UriBuilder uri = new UriBuilder(ctx.Request.Url);
uri.Host = WebConfigurationManager.AppSettings["FullHost"];
// 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:
response.AddHeader("Location", uri);
response.StatusCode = 301;
response.StatusDescription = "Moved Permanently";
response.End();
}
}
}
}
Poi registrare la classe nel web.config:
<httpModules>
[...]
<add type="[Namespace.]SeoUrls, [AssemblyName], [Version=x.x.x.x, Culture=neutral, PublicKeyToken=933d439bb833333a]" name="SeoUrls"/>
</httpModules>
Questo metodo funziona abbastanza bene per noi.
fonte
2009-02-06 21:25:29
questo è chiamato il "nome canonico", se si desidera guardare in su altrove. –
È importante tenere presente che se non si utilizza un www (o qualche altro sottodominio) tutti i cookie verranno inviati a tutti i sottodomini e non sarà possibile avere un sottodominio senza cookie per servire contenuto statico riducendo così la quantità di dati inviati avanti e indietro tra il browser e il server. Qualcosa di cui potresti in seguito rammaricarti: http://twitter.com/codinghorror/statuses/1637428313 –
@Diodeus - Abbiamo davvero bisogno di un tag per questo? Correzione – theycallmemorty