Beschreibung:
Um Duplicate Content zu vermeiden könnt ihr folgendes HttpModule benützen.
Meine Methode fügt jeder URL die kein www besitzt eines hinzu.
Die Zeile für den Developmentserver kann entfernt werden wenn ihr die seite nicht mit dem Cassini testet.
public class DuplicateContentModule : IHttpModule
{
private static Regex regex = new Regex("(http|https)://www\\.", RegexOptions.IgnoreCase | RegexOptions.Compiled);
public void Dispose() { }
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
Uri url = application.Context.Request.Url;
bool hasWWW = regex.IsMatch(url.ToString());
if (!hasWWW)
{
//set filter to run on development server
if (url.ToString().Contains("localhost")) { return; }
// Insert www
string newUrl = String.Empty;
if (url.ToString().Contains("https://"))
{
newUrl = url.ToString().Replace("https://", "https://www.");
}
else
{
newUrl = url.ToString().Replace("http://", "http://www.");
}
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Status = "301 Moved Permanently";
HttpContext.Current.Response.AddHeader("Location", newUrl);
}
}
}
Schlagwörter: Duplicate Content, ASP.net
Deine Methode funktioniert so nicht bei einer https Verbindung. Da fehlt noch
string newUrl = url.ToString().Replace("https://", "https://www.");
Gruß pdelvo