Sitecore-managed websites display pages using URLs that reflect the content tree. This is a really nice feature that provides SEO benefits and logical mappings from the tree to actual pages. By default, these pages contain a
.aspx file extension. Though this is the default behavior, it is not necessary. This article covers the various approaches to remove the
.aspx extension. I’m writing this in part because I’ve done this myself before and went through some hurdles based on my configuration and I also still see questions about this subject.
The Sitecore LinkProvider
The page URLs from Sitecore-managed sites don’t actually map to physical files and folders of the same names. They’re dynamically created URLs that are processed into the Sitecore pipeline to determine what content item to put in context. This is a fantastic feature since it provides SEO benefits and logically maps content tree architecture to the URL structure. There are several things going on here. I don’t know the inner details but Sitecore knows, given a specific URL, how to access the correct item from the database. Sitecore’s LinkProvider also knows, based on settings, how to create dynamic links to point to content items (i.e. pages in this case). The trick to removing the
.aspx extension is to tell the link provider not to add it when it creates links. When that’s done, any call to
LinkManager.GetItemUrl() will return the friendly SEO URL minus the extension. In addition, Sitecore needs to process URL requests, so you might need to tweak some settings to make all requests go into the Sitecore pipeline.
Implementing Extension Removal
There are several ways to go about removing the
.aspx extension. The approaches vary primarily based on the version of Sitecore and the version of IIS being used. The easiest way to accomplish extension removal is to run your site in Integrated mode (IIS7+) with Sitecore 6.2+. Once you do this, all requests will go through ASP.NET. To test it out, change your app pool to Integrated, then go to a page on your Sitecore site. Literally remove the
.aspx and load the new URL and you will see the page still loads! That’s pretty easy, but you’ll notice dynamically generated Sitecore URLs still come with the extension (e.g. when you link to another item in a rich text field). That’s a fairly quick change to make. In the
web.config find the LinkProvider section and change the
addAspxExtension attribute to false:
<add name="sitecore" type="Sitecore.Links.LinkProvider, Sitecore.Kernel" addAspxExtension="false" alwaysIncludeServerUrl="false" encodeNames="true" languageEmbedding="never" languageLocation="filePath" shortenUrls="true" useDisplayName="false" />
Now when calls to the LinkProvider are made, such as
LinkManager.GetItemUrl(), URLs will be returned without the extension. Now you’ve made dynamic URLs get generated without the extension and you’ve made all requests go through Sitecore, so it can determine the context item.
Sitecore officially began supporting Integrated pipeline mode in version 6.2. If you cannot run your Sitecore in Integrated mode, keep reading.
Override GetItemUrl() in the LinkProvider
The SDN has a scrapbook entry on a way to override the
GetItemUrl() method in the LinkProvider to remove the
.aspx extension. This is a nice solution for older versions of Sitecore. You can read it directly on the SDN: Friendlier Marketing URLs for Sitecore 6. To summarize the approach:
- Configure the IIS 404 error to map to the Sitecore web root’s default.aspx file to force Sitecore to process all URL requests.
- Create an assembly that inherits the LinkProvider and overrides GetItemUrl() to remove the .aspx from the URL
- Build the new assembly, deploy it into the app, and modify the linkManager section to use the new assembly for the Sitecore LinkProvider
- Add a new attribute to your
The steps above described in detail on the SDN will essentially force Sitecore to process requests to all pages (even extensionless folder-like requests), and will override the LinkProvider to supply back item URLs without the .aspx on the end.