Laden...

[ASP.net] Duplicate Content

Erstellt von Jabi vor 14 Jahren Letzter Beitrag vor 14 Jahren 2.833 Views
J
Jabi Themenstarter:in
222 Beiträge seit 2006
vor 14 Jahren
[ASP.net] Duplicate Content

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

1.346 Beiträge seit 2008
vor 14 Jahren

Deine Methode funktioniert so nicht bei einer https Verbindung. Da fehlt noch

 string newUrl = url.ToString().Replace("https://", "https://www.");

Gruß pdelvo

J
Jabi Themenstarter:in
222 Beiträge seit 2006
vor 14 Jahren

Hallo,

Stimmt das habe ich nicht berücksichtigt.