IIS7 Extensionless Urls and Kentico CMS 5.0

Posted by: admin | Technical | 29.01.2010

This post has been updated and is a wee bit out of date, see here for the updated version:

http://www.markerstudio.com/technical/2010/03/iis7-extensionless-urls-seo-and-kentico-5–0/

Base System Require­ments: IIS 7.0 with .net 3.5 SP1 on a Windows Vista dev machine or Windows 2008 server to utilise exten­sion­less urls.

Firstly, install the marvel­lous 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, all you have to do is tell Kentico not to use exten­sions on post­back urls. I had to delve into the actual source code to locate this little gem of a setting. Set it to false and stick in AppSettings.config to ensure Kentico will not write out form actions which end in .aspx.

CMSUse­Ex­ten­sionOn­Post­back = false

Next, make sure Kentico is config­ured to not add friendly url exten­sions when it gener­ates urls.

  • Site Manager > Settings > Urls>
  • Ensure the Friendly Url Exten­sions value is blank

Now all you need to do now is apply the rewrit­ing rules  within the web.config, util­is­ing the IIS Rewrite Module’s capa­bil­i­ties. (Note that you don’t need to do anything within IIS, you can manage every­thing within the web.config and simply deploy it out to target servers with­out worry­ing about any addi­tional configuration).

For exam­ple, the Enforce­Trail­ingSlash rule below will ensure that a trail­ing slash is added to all urls with­out exten­sions, perform­ing a 301 redi­rect in the process. This is so to avoid multi­ple urls (ones with and with­out slashes) return­ing the same content which isn’t ideal from an SEO perspective.

The Trail­ingSlash­ToAspx rule below will ensure that a trail­ing slash is rewrit­ten inter­nally to .aspx, so that the kentico rewrit­ing engine can take over. Note that the Kentico CMS fold­ers are ignored (ones start­ing with CMS..)

<system.webServer>
<rewrite>
<rules>
<rule name=”EnforceTrailingSlash”>
<match url=”^(.*)$” ignoreCase=”false” />
<condi­tions>
<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=”(.*)\.([a-zA-Z]+)(\?.*)?$” />
<add input=”{REQUEST_URI}” negate=“true” pattern=”(.*)/(\?.*)?$”/>
</conditions>
<action type=”Redirect” url=”{R:1}/” redirectType=”Permanent” />
</rule>

<rule name=”TrailingSlashToAspx”>
<match url=”^(.*)/$” />
<condi­tions>
<add input=”{REQUEST_FILENAME}” matchType=”IsFile” negate=”true” />
<add input=”{REQUEST_URI}” negate=”true” pattern=”/cms(.*)$” />
<add input=”{REQUEST_URI}” negate=“true” pattern=”/getattachment/(.*)$”/>
<add input=”{REQUEST_URI}” negate=“true” pattern=”/getfile/(.*)$”/>
</conditions>
<action type=”Rewrite” url=”{R:1}.aspx” />
</rule>
</rules>
</rewrite>
</system.webServer>

And so there you have it, a rela­tively pain­less way to get your­self all SEOd with IIS 7.0 and Kentico CMS 5.0.

So what about the Kentico 5.0 SEO Features? mmm…maybe not

Kentico makes avail­able 2 settings in v5.0 which are “Always apply trail­ing slash to urls” and “Always lower case url names” which in theory sound great, but i still recom­mend the above approach for control.

The first one only really makes sense if you are using exten­sion­less urls anyway (which you need to config­ure sepa­rately from this setting, see below). When you set “Always apply trail­ing slash to urls” this ensures all urls gener­ated by Kentico end with a trail­ing slash, which is a good thing. However, it also means that Kentico will attempt to add a trail­ing slash to our rewrit­ten url above!

When this happens you get an infi­nite loop occur­ing where the rewrite module is attempt­ing to rewrite the url to .aspx for Kentico to process and for some reason (and this is clearly in my view a bug) Kentico is attempt­ing to put a trail­ing slash on a rewrit­ten url which ends in an aspx. Kentico should IMHO leave this already rewrit­ten url alone and only apply 301s and trail­ing slashes if the url actu­ally requires it.

We’re still there­fore wait­ing on a solu­tion that just has Kentico gener­ate all urls with trail­ing slashes and noth­ing else!

Upon inves­ti­ga­tion with support, Kentico still appear to recom­mend that you use the old IIS 6.x approach of assign­ing custom 404 error page to a Kentico handler page in order to imple­ment exten­sion­less urls. If you are using IIS 6, by all means use this approach if you must, but i don’t really like the extra config­u­ra­tion effort and it’s always felt a bit hacky to do this. 404 errors should be gener­ated when a page is actu­ally not found. This older approach is obvi­ously not compat­i­ble with what i’m suggest­ing above.

Another prob­lem we discov­ered with the Kentico SEO settings was when used in combi­na­tion with alias­ing. Alias­ing is a Kentico feature wherby you can speci­fiy a url such as /articles/{article-title}, where {article-title} is any piece of text (the alias) and that these alias url requests all point to some under­ly­ing url such as /articles/article/.

If you switch on the “Always lower case url names” and you request a dynamic alias such as /articles/MY-ARTICLE/ it will redi­rect to the under­ly­ing url /articles/article/ break­ing the alias­ing approach. This appear to be a core bug in kentico 5.0.

One Response to “IIS7 Extensionless Urls and Kentico CMS 5.0”

Leave a Reply