Modifying Web.config to Serve Site.xml and Static Files with Orchard CMS

Modifying Web.config to Serve Site.xml and Static Files with Orchard CMS

I received an email from an Orchard Web Developer wanting to know how to serve his site.xml file from his Orchard CMS Website. If you add a site.xml file or other static file to the root of your Orchard CMS Website you will get an HTTP 404 Not Found Error. This isn't so much an Orchard CMS question as it is an ASP.NET and Web.config question, because the answer lies in the web.config file for Orchard which is currently configured to respond to all static file requests at the root with a 404 Error if there is no route configured in ASP.NET MVC to handle the request.

Site.xml and Root Web.config in Orchard CMS

If you take a peek at the web.config at the root of an Orchard Website, you will notice that all the handlers for the website are cleared and that the only handler configured is the System.Web.HttpNotFoundHandler that responds to all requests. This means that even though you placed a site.xml file at the root of your Orchard Website, your IIS6 or IIS7 Website will return a File Not Found Error for the request. The only way to have the Orchard Website respond to a request for the site.xml file is to modify the <httpHandlers> and/or <system.webServer><handlers> section of the web.config. Best to just make a change in both places so the solution works on both IIS6 and IIS7.

My recommendation is to be as strict as possible and create an http handler that responds just to the site.xml request using the StaticFileHandler and StaticFileModule for IIS6 and IIS7. Assuming you have placed a proper site.xml file at the root of your Orchard Website, you can make a couple of changes to the web.config file so that it is served by IIS and used by Google, Bing, and any other search engines that may use the site.xml to help properly add your Orchard Web Pages to the search engine's results. Note that I have removed some items from the Orchard Web.config file for aesthetic purposes and this Web.config file may be different depending on your needs and the version of Orchard CMS. It is just here for illustration purposes.

Http Handler for Orchard CMS and IIS6 for site.xml

I think the new settings in the Orchard Web.config are pretty self-explanatory. Before the setting to return File Not Found you want to place a handler that tells IIS6 and IIS7 to serve site.xml if it is requested by the Google, Bing, or other search engine. If you place it after the HttpNotFoundHandler it won't work. It must be before it. The StaticFileHandler and StaticFileModule are responsible for returning a static file, such as site.xml, when a browser, search crawler, etc. requests a static file. It's also important that the name of the http handler, "SiteXml", is unique and doesn't clash with another name of the handler in the website. I would name it something obvious and specific, like "SiteXml", since it is specifically used to return the site.xml file for the Orchard Website.

Site.xml File Custom Orchard Module

Just a quick note that hopefully you are using this in conjuction with a custom Orchard Module that automatically generates a site.xml file. As an Orchard Developer or Orchard Website Owner you want to be able to create web pages on the fly and have a custom Orchard Module generate the site.xml file for you either in real-time or manually based on routable content types in your Orchard Website. You don't want to have to manually add pages to the site.xml in your Orchard Website everytime you decide to add a new page or routable content type. I am sure there is a custom Orchard Module to generate a site.xml file in the Orchard Gallery. I created my own custom Orchard Module and use it to generate a site.xml file if I feel the file is necessary.

Conclusion

Alrighty, I think I beat this Orchard Tutorial to death :) Hopefully it helps other Orchard Web Developers if they also need to serve a site.xml or other static file in an Orchard CMS Website. If you are looking for an Orchard Web Developer, I am available for hire.

Hire Me