1. Create a class library project in "New Project": httmmodules
Implementation code in the class file:
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using Microsoft. SharePoint;
Using Microsoft. Sharepoint. publishing;
Using Microsoft. Sharepoint. webcontrols;
Using system. Web;
Using system. Text. regularexpressions;
Namespace imtech. Sharepoint. enhancement. httpmodules
{
Public class redirectmodule: ihttpmodule
{
# Region ihttpmodule members
Public void dispose (){}
Public void Init (httpapplication context)
{
Context. beginrequest + = new eventhandler (context_beginrequest );
}
Void context_beginrequest (Object sender, eventargs E)
{
Httpapplication APP = (httpapplication) sender;
String requesturl = app. Request. url. tostring ();
RegEx = new RegEx (@ "^ HTTPS? ://.*(? <Itemurl>/[^/] +. [^/.] +) $ ");
If (RegEx. ismatch (requesturl) return;
If (! Requesturl. endswith ("/", stringcomparison. currentculture) requesturl + = "/";
String destinationurl = string. empty;
Spsecurity. runwithelevatedprivileges (delegate ()
{
Try
{
Using (spsite site = new spsite (requesturl ))
{
Using (spweb web = site. openweb ())
{
If (publishingweb. ispublishingweb (Web ))
{
Publishingweb =
Publishingweb. getpublishingweb (Web );
If (publishingweb. defaultpage. url. endswith ("/variationroot. aspx", stringcomparison. currentcultureignorecase ))
{
String defaultpage = string. empty;
Using (spweb nlweb = site. openweb ("NL "))
{
Defaultpage = publishingweb. getpublishingweb (nlweb). defaultpage. url;
}
Destinationurl = string. Concat (requesturl, "NL/", defaultpage );
}
Else
Destinationurl = string. Concat (requesturl, publishingweb. defaultpage. url );
}
Else
Destinationurl = string. Concat (requesturl, "default. aspx ");
}
}
}
Catch {}
});
If (! String. isnullorempty (destinationurl ))
{
App. response. addheader ("location", destinationurl); app. response. statuscode = 301;
}
}
# Endregion
}
} 2. Copy the class library httpmodule. DLL to the bin directory of the website. 3. Configure in Web. config: <Add name = "imtechredirectmodule" type = "imtech. Sharepoint. enhancement. httpmodules. redirectmodule"/>
</Httpmodules>