IIS7 Extensionless Urls and Kentico CMS 4.0

Disclaimer: This post contains unsupported guideline for configuring your Kentico CMS 4.x to utilise extensionless urls with IIS 7.0. Use with care!

Base System Requirements: IIS 7.0 with .net 3.5 SP1 on a Windows Vista dev machine or Windows 2008 server to utilise extensionless urls.

Firstly, install the marvellous IIS 7.0 URL Rewrite Module
Get it here: http://www.iis.net/downloads/default.aspx?tabid=34&g=6&i=1692
Learn about it here: http://learn.iis.net/page.aspx/460/using-url-rewrite-module/

Now, make sure Kentico is configured not to interefere with things…

Kentico automatically applies a filter to the form tags. You need to disable this here:

  • Site Manager > Settings > Output Filters >
  • Ensure the ‘Excluded output form filter URLs’ value is set to a single forward slash.. /
  • Site Manager > Settings > Urls>
  • Ensure the Friendly Url Extensions value is blank

There is currently no way of ensuring that automatically generated urls from Kentico contain a trailing slash. The user must set the Document UrlPath for each page in Page > Properties > Urls to include the trailing slash. However the trailing slash is enforced via in IIS 7 rewriting rule. It is hoped that a future version of Kentico will allow for this to avoid the extra 301 redirect.

Now apply the following website configuration….

Place the following code in the Page_Load of your master page.
            // ensure we fix up the form action if required
            if (!String.IsNullOrEmpty(Request.ServerVariables[“HTTP_X_ORIGINAL_URL”]))
                form1.Action = Request.ServerVariables[“HTTP_X_ORIGINAL_URL”];

This ensures that Asp.Net postback and ajax work correctly with the IIS7 URL Rewrite Module

Now all you need to do now is apply whatever special rewriting rule syou like within the web.config, utilising the IIS Rewrite Module’s capabilities as you need them. (Note that you don’t need to do anything within IIS, you can manage everything within the web.config and simply deploy it out to target servers without worrying about any additional configuration).

For example, the EnforceTrailingSlash rule below will ensure that a trailing slash is added to all urls without extensions, performing a 301 redirect in the process. This is so to avoid multiple urls (ones with and without slashes) returning the same content which isn’t ideal from an SEO perspective.

The TrailingSlashToAspx rule below will ensure that a trailing slash is rewritten internally to .aspx, so that the kentico rewriting engine can take over. Note that the Kentico CMS folders are ignored (ones starting with CMS..)

                <rule name=”EnforceTrailingSlash”>
                    <match url=”^(.*)$” ignoreCase=”false” />
                        <add input=”{REQUEST_FILENAME}” matchType=”IsFile” negate=”true” />
                        <add input=”{REQUEST_URI}” negate=”true” pattern=”(.*).([a-zA-Z]+)(?.*)?$” />
                        <add input=”{REQUEST_URI}” negate=”true” pattern=”(.*)/$” />                      
                    <action type=”Redirect” url=”{R:1}/” redirectType=”Permanent” />

                <rule name=”TrailingSlashToAspx”>
                    <match url=”^(.*)/$” />
                        <add input=”{REQUEST_FILENAME}” matchType=”IsFile” negate=”true” />
                        <add input=”{REQUEST_URI}” negate=”true” pattern=”/cms(.*)$” />
                        <add input=”{REQUEST_URI}” negate=”true” pattern=”^(.*)/.aspx[#w=|&amp;%-]*$” />
                    <action type=”Rewrite” url=”{R:1}.aspx” />

And so there you have it, a relatively painless way to get yourself all SEOd with IIS 7.0 and Kentico CMS 4.0.

5 comments on “IIS7 Extensionless Urls and Kentico CMS 4.0

  1. Ruslan said on 7 April 2009 at 8:54 am:

    Instead of HTTP_X_ORIGINAL_URL it is recommended to use Request.RawUrl property to get the original URL. See more details here: http://ruslany.net/2008/10/aspnet-postbacks-and-url-rewriting/

  2. Keith Patton said on 9 April 2009 at 10:53 am:

    HTTP_X_ORIGINAL_URL is specific to the IIS 7 Rewrite Module and i believe is safe to use. Request.RawUrl may work as well and would certainly be the one to use if you weren’t using the II7 Rewrite module.

  3. Michiel said on 29 May 2009 at 10:22 pm:

    Will this work attachments? In Kentico these are requested through ~/getattachment/ followed by a guid and a trailing slash.

  4. Will said on 13 June 2009 at 9:28 am:

    Here’s how you achieve this in Umbraco.

  5. dan said on 1 October 2009 at 2:49 pm:

    Saved my bacon 😉

Comments are closed.