Displaying Custom 404 error page for urls with unallowed file extension in sitecore


Hey, there! Welcome to "Sitecore Series"

Post is all about displaying custom 404 page for unallowed file extension requests.

In Sitecore, we have an option to allow required file extensions and only the allowed file extensions are processed by Sitecore. (Not Sure? How to configure file extensions? Refer my previous post on allowing or blocking specific file extensions in Sitecore)

When the custom 404 page is configured, Sitecore will process the URL and maps it with the associated file in webroot or item in Sitecore and displays it. If the file/item is not available, then it will display a custom 404 page.

In case of unallowed file extensions, Sitecore will not process the request. Request is directly handled by IIS and redirects to default error page. Oh no! user is seeing default error page, not a good user experience. 

Want to know how to configure a custom error page in this scenario? Yeah, this post is for you.

 Refer below for the solution:

Existing custom 404 page configuration will not work as the file resolver or item resolver pipelines are not executed as sitecore will not handle unallowed file extension Url requests.

So the fix to be done from IIS or to edit the web config file directly.

To add the fix in web.config file:

1) Access the web.config file from webroot.

2) Find out the "<system.webserver>" section.

3) Add the below setting inside <system.webserver> ( append just before the closing tag </system.webserver>):


<httpErrors errorMode="Custom">
<remove statusCode="404" />
<error statusCode="404" path="/404" responseMode="ExecuteURL" />
</httpErrors>

Note: errorMode should be Custom and responseMode can be either a URL, Redirection or a File.


If the custom error page is added in webroot, then update responseMode as File:


<httpErrors errorMode="Custom">
<remove statusCode="404" />
<error statusCode="404" path="404.html or 404.aspx or 404.htm" responseMode="File" />
</httpErrors>

Note: Path varies if 404 page is inside any subfolder.

To add the fix from IIS:

1) Open IIS

2) Expand Sites -> select your site

3) Find Error Pages Option -> Double click on "Error Pages"

4) List of error pages will open -> Double click on "404" page

5) Choose the option as per your wish and configure the file/redirect or URL -> click ok.

Once done, make sure to check web.config and see whether errorMode ="Custom" or not. If not, update it to Custom.

Now hit your site url with any unallowed file extension, you will see the custom 404 error page.


Hope this post helps you. Happy Sitecoring.. 😊

Comments

Popular posts from this blog

Overwriting sxa content field in solr document

How to specify custom richtext menu as default richtext field setting when no source is specified for richtext field in sitecore

How to rebuild custom solr index programmatically in Sitecore

Sitecore - Task Schedulers and how to schedule a task

Handling URL requests with different file extensions in Sitecore

Accessing Items from Sitecore Database in different scenarios