Handle any requests and any extensions in Sitecore

In this article, we will look how to Handle any requests and any extensions in Sitecore without writing any code. With some configuration changes, we could easily allow Sitecore to process all the file extensions.

By Default Sitecore allows only “.aspx and .asmx” extensions during the request. If any other extension is tried then it will not hit the code rather the IIS will throw a 404 error saying file not found. So let’s say if you need to implement 301 redirections from legacy .java or .php extensions. How exactly could you make this work?

Handle any request and any extensions in Sitecore

 <preprocessRequest help="Processors should derive from Sitecore.Pipelines.PreprocessRequest.PreprocessRequestProcessor">
        <processor type="Sitecore.Pipelines.PreprocessRequest.SuppressFormValidation, Sitecore.Kernel" />
        <processor type="Sitecore.Pipelines.PreprocessRequest.NormalizeRawUrl, Sitecore.Kernel" />
        <processor type="Sitecore.Pipelines.PreprocessRequest.IIS404Handler, Sitecore.Kernel" />
        <processor type="Sitecore.Pipelines.PreprocessRequest.FilterUrlExtensions, Sitecore.Kernel">
          <param desc="Allowed extensions (comma separated)">aspx, ashx, asmx</param>
          <param desc="Blocked extensions (comma separated)">*</param>
          <param desc="Blocked extensions that stream files (comma separated)">*</param>
          <param desc="Blocked extensions that do not stream files (comma separated)">
          </param>
        </processor>
        <processor type="Sitecore.Pipelines.PreprocessRequest.StripLanguage, Sitecore.Kernel" />
      </preprocessRequest>

If we closely look into the Sitecore web.config file, the default extensions allowed are .aspx, .ashx and .asmx files. All the other extensions are blocked. Hence the IIS will not handle any other extensions and the default 404 error page will be shown when we hit any other extensions.

In order to handle any file extensions and any request in Sitecore just remove the “*” from <param desc=”Blocked extensions (comma separated)”>*</param> and place the * in the <param desc=”Allowed extensions (comma separated)”>*</param>.

The updated config file section will look similar to the snippet mentioned below.

 <preprocessRequest help="Processors should derive from Sitecore.Pipelines.PreprocessRequest.PreprocessRequestProcessor">
        <processor type="Sitecore.Pipelines.PreprocessRequest.SuppressFormValidation, Sitecore.Kernel" />
        <processor type="Sitecore.Pipelines.PreprocessRequest.NormalizeRawUrl, Sitecore.Kernel" />
        <processor type="Sitecore.Pipelines.PreprocessRequest.IIS404Handler, Sitecore.Kernel" />
        <processor type="Sitecore.Pipelines.PreprocessRequest.FilterUrlExtensions, Sitecore.Kernel">
          <param desc="Allowed extensions (comma separated)">*</param>
          <param desc="Blocked extensions (comma separated)"></param>
          <param desc="Blocked extensions that stream files (comma separated)">*</param>
          <param desc="Blocked extensions that do not stream files (comma separated)">
          </param>
        </processor>
        <processor type="Sitecore.Pipelines.PreprocessRequest.StripLanguage, Sitecore.Kernel" />
      </preprocessRequest>

Handle Specific Extensions in Sitecore

Many times you need to handle only specific extensions types in Sitecore. For Eg: Let’s say you  need to implement 301 redirections on the legacy PHP pages to new Sitecore pages. The extension of the legacy site will end with .php always. In order to allow specific extensions just add the extension in the config section as shown below.

Once you add the extension the Sitecore is capable of handling the requests. You could override the HttpRequestProcessor pipeline and implement the 301 redirections in Sitecore

 <preprocessRequest help="Processors should derive from Sitecore.Pipelines.PreprocessRequest.PreprocessRequestProcessor">
        <processor type="Sitecore.Pipelines.PreprocessRequest.SuppressFormValidation, Sitecore.Kernel" />
        <processor type="Sitecore.Pipelines.PreprocessRequest.NormalizeRawUrl, Sitecore.Kernel" />
        <processor type="Sitecore.Pipelines.PreprocessRequest.IIS404Handler, Sitecore.Kernel" />
        <processor type="Sitecore.Pipelines.PreprocessRequest.FilterUrlExtensions, Sitecore.Kernel">
          <param desc="Allowed extensions (comma separated)">aspx, ashx, asmx, php</param>
          <param desc="Blocked extensions (comma separated)">*</param>
          <param desc="Blocked extensions that stream files (comma separated)">*</param>
          <param desc="Blocked extensions that do not stream files (comma separated)">
          </param>
        </processor>
        <processor type="Sitecore.Pipelines.PreprocessRequest.StripLanguage, Sitecore.Kernel" />
      </preprocessRequest>
Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like