SharePoint Request Code 302 is 301

Source: Internet
Author: User

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>

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.