Asp.net uses the httpmodule of Web. config to achieve permanent redirection of the entire site 301 (Simple and Convenient)

Source: Internet
Author: User

Before doing 301 ing is to call a method on each page, the original http://www.cnblogs.com/hakuci/archive/2010/11/19/1881681.html

Now, the httpmodule of Web. config is used to achieve permanent redirection of the entire site 301.

The specific method is as follows:
1. Add configuration in Web. config

<Configuration>
<Deleetask>
<Add key = "webdomain" value = "wecanwecan.com"/>
<Add key = "url301location" value = "www.wecanwecan.com"/>
</Appsettings>

2. Create a class library project under the current solution
3. Create a new CS. I will give it a rough name here: changedomain. CS

Using system;
Using system. Web;

Using system. configuration;

Namespace changedomain
{

Public class redirectnewdomain: ihttpmodule
{
Public void dispose ()
{
}
Public void Init (httpapplication context)
{
Context. authorizerequest + = (New eventhandler (process301 ));
}
Public void process301 (Object sender, eventargs E)
{
Httpapplication APP = (httpapplication) sender;
Httprequest request = app. Context. request;
String lrequestedpath = request. url. dnssafehost. tostring ();
String strdomainurl = configurationmanager. receivettings ["webdomain"]. tostring ();
String strweburl = configurationmanager. receivettings ["url301location"]. tostring ();
If (lrequestedpath. indexof (strweburl) =-1)
{
App. response. statuscode = 301;
App. response. addheader ("location", lrequestedpath. replace (lrequestedpath, "http: //" + strweburl + request. rawurl. tostring (). trim (); // The domain name here is modified based on your actual situation
App. response. End ();
}
}
}
}

After writing this, you will be OK. The rest is to register in Web. config.

 

<Httpmodules>
<Add name = "changedomain" type = "changedomain. redirectnewdomain, changedomain"/>
</Httpmodules>

The above naming is also a rough name on my side. The specific Web. config registration method is as follows:

 

<Add name = "name" type = "httpmodule namespace plus class name, DLL file name"/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.