Thinking

IIS7 Extensionless Urls, SEO and Kentico 5.0

My previous post on this subject only went so far. It focused solely on the usage of extensionless urls with Kentico but stopped short of full SEO compliance because of problems experienced when trying to integrate Kentico SEO settings with the IIS7 rewrite module. This post completes the by getting the SEO settings fully implemented as well.

The IIS SEO Toolkit allows you to analysis many aspects of your sites quality and performance with particular focus on SEO weaknesses, which are shown up as ‘violations’ and ‘warnings’. You will find that if you are using Kentico that implementing the guidelines in this post will dramatically reduce the number of those you will get when running the tool. Note that anyone can run this toolkit as long as they are running Windows and it can run against local and remote websites.

In the AppSettings section of your web.config, set the following setting to ensure Kentico will not write out form actions which end in .aspx (which it does by default).

CMSUseExtensionOnPostback = false

Next, make sure Kentico is configured for extensionless urls and has the appropriate SEO settings configured as follows:

Site Manager > Settings > URLS and SEO >

Ensure the ‘Friendly URL Extensions’ value is blank

Ensure the ‘Files friendly URL Extensions’ value is ‘/’ (slash) *

Ensure the ‘Use Urls With Trailing Slash’ value is set to ‘Always use URLs with trailing slash’

Ensure the ‘Redirect invalid case URLs to their correct versions’ value is set to ‘Redirect all requests to lower case’

* Kentico treats a blank Files Friendly Extension value to mean it should use the extension of the file, so you would get /getattachment/{guid}/file.jpg for example. This is a little confusing because by default .net does not process these extensions so 404s result all over the place. Adding the slash means that your files have the url /getattachment/{guid}/file.jpg/ which will get processed fine. If you really want you can leave it blank and configure IIS to process all the relevant static extensions you require for files, but i find that more trouble than it’s worth and find the slash based file url to be fine from an SEO perspective.

Now all you need to do now is apply the rewriting rules within the web.config, utilising the IIS Rewrite Module’s capabilities. The great thing is 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.

The first rule below ensures that a trailing slash is added to all url requests unless they already have one or have an extension already for whatever reason (e.g. asmx ajax requests)

The second rule rewrites the trailling slash url to an aspx request to be processed by Kentico. (Note: The addition of the trailing slash on the rewritten url is to stop Kentico trying to append the trailing slash to the rewritten url, which i noted in my previous post).

The third rule is a special one for processing the home page request and rewriting this as if it were a trailing slash request.

Kentico does not enforce lower case urls when documetns are added or updated. This is important because otherwise urls will get rendered in mixed case and then 301 redirects will be performed to get to the lower case versions. It is hoped this is addressed in a future version of Kentico, but in the mean time we can use the Kentico custom event handler capability to deal with this ourselves.

You will need to check out the Kentico event handler documentation to see how to set up custom event handlers for your project. Here is teh code to add to the CustomTreeNodeHandler to ensure that when anything is added or updated, the NodeAlias propety (and therefore the url) is lower-cased. Note that doing this in OnBeforeInsert didn’t work ffor inserts or some reason, so i found the only way to get it to work was to update the node straight after it was inserted. A double call to the database i guess, but not too bad, Kentico are investigating.
[sourcecode language=”csharp”]
public override void OnAfterInsert(object treeNodeObj, int parentNodeId, object tree)
{
// ensure node alias is lower cased
TreeNode node = (TreeNode)treeNodeObj;
node.Update();
}