Web developers wiki ASP.NET Sitecore Sharepoint Kentico by Evident Interactive

SC Version Tested on 6.4 & 6.5

Case description: We encountered an issue with HTTP statuscodes (no real 404 code returned) on a Sitecore 6 site with a custom 404 page on IIS7 (integrated mode). This also occurs on Sitecore solutions with the default 404 page. Following this article will results in a true 404 statuscode on the original url (no visible redirect) that someone visits and the page can be CMS editable.

Steps:

1. Implement ExecuteRequest pipeline at the bottom of this article.
2. Create a config include file to engage pipeline.
3. Create a 404 page in the CMS (quicktip: use a detail template and add an extra control to set "404" statuscode)
4. Point to the right "Not Found" page in your Settings.config (web.config patch file):
5. Set UseServerSideRedirect to true in web.config (using include file)


Not found setting

			< setting name="ItemNotFoundUrl" >
				< patch:attribute name="value">/NotFoundPage
			< /setting >

Code for the 404 control (add it to a detail page)

	Response.StatusCode = 404;


Pipeline:




class ExecuteRequest : Sitecore.Pipelines.HttpRequest.ExecuteRequest
	{
		/// 
		/// Performs a redirect for the specified URL
		/// 
		/// 
		protected override void PerformRedirect(string url)
		{
			if (Sitecore.Configuration.Settings.RequestErrors.UseServerSideRedirect)
			{
				Sitecore.Web.WebUtil.RewriteUrl(url);
				Context.Item = GetItemFromUrl(url);
				if (Context.Item != null)
				{
					HttpRequestArgs args = new HttpRequestArgs(HttpContext.Current, HttpRequestType.Begin);
					Sitecore.Pipelines.CorePipeline.Run("httpRequestBegin", (Sitecore.Pipelines.PipelineArgs)args);
				}
			}
			else
			{
				WebUtil.Redirect(url, false);
			}
		}

		/// 
		/// Returns an Item from a URL.
		/// 
		///The url of the sitecore item.
		///The Item the url represents.
		public static Item GetItemFromUrl(string url)
		{
			string referring_item_path = null;
			if (Uri.IsWellFormedUriString(url, UriKind.Relative))
				url = Sitecore.Web.WebUtil.GetFullUrl(url);
			if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
			{
				Uri uri = new Uri(url);
				if (uri.AbsolutePath.StartsWith("/sitecore/"))
					referring_item_path = uri.AbsolutePath;
				else
					referring_item_path = Sitecore.Context.Site.StartPath + uri.AbsolutePath;
			}

			//Remove the extension and put a '/' instead.
			referring_item_path = Regex.Replace(referring_item_path, @"(?:\.aspx|\.ashx)", "/");

			//Declare a temp return value variable.
			Item retVal = null;
			using (new Sitecore.SecurityModel.SecurityDisabler())
			{
				//Get the item based off the sitecore path.
				if (Sitecore.Context.Database != null)
				{
					retVal = Sitecore.Context.Database.GetItem(referring_item_path);
				}
			}

			//return the item.
			return retVal;
		}
	}




Pipeline config (in include file)




< configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
	< sitecore>
		< pipelines>


			< httpRequestBegin>
				
        < processor type="Sitecore.Pipelines.HttpRequest.ExecuteRequest, Sitecore.Kernel">
          < patch:attribute name="type">SCCustom.Library.Pipelines.ExecuteRequest, SCCustom.Library
        < /processor>
			< /httpRequestBegin>







Set UseServerSideRedirect to true

	
			< setting name="RequestErrors.UseServerSideRedirect">
				< patch:attribute name="value">true< /patch:attribute>
			< /setting>

 © Evident Interactive BV