{ Dev Farm }

Web & Windows Development

Ready to use 301 Redirect with Http Module

| 1 commento

3.00 avg. rating (72% score) - 1 vote

Just open the web.config and edit the appsetting newSite with your new site. That’s all!

All the requests will be forwarded with “301 Moved Permanently” status code.

Example: Your site is published at http://www.currentsite.com and you want to redirect at http://www.newsite.com

upload the files at http://www.currentsite.com and edit the web.config like:

www.newsite.com"/>

All request will be forwarded like:

http://www.currentsite.com
-> http://www.newsite.com

http://www.currentsite.com/helloworld
-> http://www.newsite.com/helloworld

 

 

The site is tested on iis7, framework 4 Integrated pipeline mode (standard configuration in most of cases).

Source Code:

——————————— Web.config

——————————— Module.cs

using System;
using System.Web;
using System.Configuration;

namespace Redirect
{
public class Module : IHttpModule
{

public void Dispose()
{
}

public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(Application_BeginRequest);
context.EndRequest += (new EventHandler(Application_EndRequest));
}

private void Application_BeginRequest(Object source, EventArgs e)
{
HttpApplication hap = (HttpApplication)source;
HttpContext hc = (HttpContext)hap.Context;
string newSite = ConfigurationManager.AppSettings["newSite"];
string strUrl = newSite + hc.Request.Url.PathAndQuery;
hc.Response.Clear();
hc.Response.Status = "301 Moved Permanently";
hc.Response.Headers.Add("Location", strUrl);
}

private void Application_EndRequest(Object source, EventArgs e)
{

}

}
}

 

Un commento

  1. Hi, Neat post. There is a problem together with your website in web explorer, would test this… IE still is the marketplace chief and a big component to other folks will pass over your excellent writing because of this problem.

Lascia un commento

I campi obbligatori sono contrassegnati con *.


:bye:  :good:  :negative:  :scratch:  :wacko:  :yahoo:  B-) 
more...