Join Now!
http://www.sharepointblogs.com/tmt/archive/2008/01/21/sharepoint-2007-redirect-solved-using-301-instead-of-302-redirects.aspx
<add name="CustomRedirectModule" type="Custom.Sharepoint.RedirectFix.RedirectModule" />
using System;using System.Collections.Generic;using System.Text;using System.Web;using System.Text.RegularExpressions;using Microsoft.SharePoint;using Microsoft.SharePoint.Publishing; namespace Custom.Sharepoint.RedirectFix{ 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 userAgent = app.Request.UserAgent.ToLowerInvariant(); if (userAgent.Contains("infopath") || userAgent.Contains("frontpage")) return; string requestUrl = app.Request.Url.ToString(); Regex 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 = PublishingWeb.GetPublishingWeb(web); destinationUrl = String.Concat(requestUrl, publishingWeb.DefaultPage.Url); } else { destinationUrl = String.Concat(requestUrl, "index.aspx"); destinationUrl = String.Concat(requestUrl, web.RootFolder.WelcomePage.ToString().Substring(web.RootFolder.WelcomePage.ToString().LastIndexOf('.') + 1)); } } } } catch { } }); if (!String.IsNullOrEmpty(destinationUrl)) { app.Response.AddHeader("Location", destinationUrl); app.Response.StatusCode = 301; } } #endregion }//class}//namespace
protected override void OnLoad(EventArgs e){ base.OnLoad(e); string targetUrl = this.GetRedirectTargetUrl(); if (!string.IsNullOrEmpty(targetUrl)) { SPUtility.Redirect(targetUrl, SPRedirectFlags.Default, Context); }}
Context.Response.Status = "301 Moved Permanently";Context.Response.AddHeader("Location",targetUrl);
protected override void OnLoad(EventArgs e){ base.OnLoad(e); string targetUrl = this.GetRedirectTargetUrl(); if (!string.IsNullOrEmpty(targetUrl)) {//SPUtility.Redirect(targetUrl, SPRedirectFlags.Default, Context);Context.Response.Status = "301 Moved Permanently"; Context.Response.AddHeader("Location",targetUrl); }}
Posted on 6/27/2008 at 9:20 AM Permalink | Share This Post | Comments (0) | Leave a Comment
Blog Tags:
All Blogs
BDC Code Community Features General Guides InfoPath Language Variations MOSS Search SharePoint Site Variations SQL Tools Variations Variations Editor Web Parts Workflow WSS