Adding Google Verification File in Orchard CMS

Whenever you want to serve a static file from your Orchard website, you will get a 404 error(page note found) error by default. This is happening for Orchard because in the web.config file the default configuration is set to return a page not found error for all the static files in the root. This will become an annoying issue whenever you try to verify the identity of the website by uploading a verification file in the root folder as I found out when I wanted to verify the identity of my site from Google Search Console.

This is issue can be resolved in two ways,

1. Configure a route in ASP.NET MVC to serve the static page.

2. Modify the web.config file in the route to return the correct page instead of 404

I will explain the steps needed to overcome this issue using the second approach. In this step, you needs to make couple of changes in the web.config file in the root directory of your Orchard site. The modifications is to be done in the sections <httpHandlers> and in <handlers> inside <system.webServer>. It is recommended to make these changes in both the places so that it works both on IIS6 and IIS7.

By default the <httpHandlers> and <handlers> sections will have the following entries in it

<httphandlers>
    <add path="*" verb="*" type="System.Web.StaticFileHandler"></add> 
</httphandlers>

<system.webServer>
<handlers accessPolicy="Script">
<add name="NotFound" path="*" verb="*" type="System.Web.HttpNotFoundHandler" preCondition="integratedMode" requireAccess="Script"/>
</handlers>
</system.webServer>

I have omitted some entries from the config file in the following snippet as it's not relevant for the topic which is being discussed in this post. Basically these two entries are telling the web server to return a page not found error whenever we request for a static file. So we will add the following entries into the config file for ignoring this rule.

<httphandlers>
<add path="file.html" verb="*" type="System.Web.StaticFileHandler" />
<add path="*" verb="*" type="System.Web.StaticFileHandler"/>
</httphandlers>

<system.webServer>
<handlers accessPolicy="Script, Read">

<add name="VerificationFile" path="file.html" verb="*" modules="StaticFileModule" preCondition="integratedMode"
resourceType="file" requireAccess="Read" />

<add name="NotFound" path="*" verb="*" type="System.Web.HttpNotFoundHandler" preCondition="integratedMode" requireAccess="Script"/>
</handlers>
</system.webServer>

Please make a note of the following points to make our solution work

  1. If you add the entries before the existing ones which says to serve the 404, then our entries doesn't have any effect. So you should always over statement above the existing one.
  2. Give Read permission in accessPolicy for handlers in <system.webServer> section. 
  3. Make sure that the name provided is unique across the handler entries in the config file.


No Comments

Add a Comment