WwwSubDomainModule.cs :  » Bloggers » BlogEngine.NET » BlogEngine.Core » Web » HttpModules » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Bloggers » BlogEngine.NET 
BlogEngine.NET » BlogEngine.Core » Web » HttpModules » WwwSubDomainModule.cs
#region Using

using System;
using System.Web;
using BlogEngine.Core;
using System.Text.RegularExpressions;

#endregion

namespace BlogEngine.Core.Web.HttpModules{
  /// <summary>
  /// Removes or adds the www subdomain from all requests
  /// and makes a permanent redirection to the new location.
  /// </summary>
  public class WwwSubDomainModule : IHttpModule
  {

    #region IHttpModule Members

    /// <summary>
    /// Disposes of the resources (other than memory) used by the module that implements <see cref="T:System.Web.IHttpModule"></see>.
    /// </summary>
    public void Dispose()
    {
      // Nothing to dispose
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="context"></param>
    public void Init(HttpApplication context)
    {
      context.BeginRequest += new EventHandler(context_BeginRequest);
    }

    #endregion

    private static Regex _Regex = new Regex("(http|https)://www\\.", RegexOptions.IgnoreCase | RegexOptions.Compiled);

    private void context_BeginRequest(object sender, EventArgs e)
    {
      if (BlogSettings.Instance.HandleWwwSubdomain == "ignore" || string.IsNullOrEmpty(BlogSettings.Instance.HandleWwwSubdomain))
        return;

      HttpContext context = (sender as HttpApplication).Context;
      if (context.Request.HttpMethod != "GET" || context.Request.RawUrl.Contains("/admin/") || context.Request.IsLocal)
        return;

      if (context.Request.PhysicalPath.EndsWith(".aspx", StringComparison.OrdinalIgnoreCase))
      {
        string url = context.Request.Url.ToString();

        if (url.Contains("://www.") && BlogSettings.Instance.HandleWwwSubdomain == "remove")
          RemoveWww(context);
        
        if (!url.Contains("://www.") && BlogSettings.Instance.HandleWwwSubdomain == "add")
          AddWww(context);        
      }
    }

    /// <summary>
    /// Adds the www subdomain to the request and redirects.
    /// </summary>
    private static void AddWww(HttpContext context)
    {
      string url = context.Request.Url.ToString().Replace("://", "://www.");
      PermanentRedirect(url, context);
    }

    /// <summary>
    /// Removes the www subdomain from the request and redirects.
    /// </summary>
    private static void RemoveWww(HttpContext context)
    {
      string url = context.Request.Url.ToString();
      if (_Regex.IsMatch(url))
      {
        url = _Regex.Replace(url, "$1://");        
        PermanentRedirect(url, context);
      }
    }

    /// <summary>
    /// Sends permanent redirection headers (301)
    /// </summary>
    private static void PermanentRedirect(string url, HttpContext context)
    {
      if (url.EndsWith("default.aspx", StringComparison.OrdinalIgnoreCase))
        url = url.ToLowerInvariant().Replace("default.aspx", string.Empty);

      context.Response.Clear();
      context.Response.StatusCode = 301;
      context.Response.AppendHeader("location", url);
      context.Response.End();
    } 

  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.