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"/>