Upgrading From Sitefinity 3: Url Rewriting

This post is part of a series exploring my migration from Sitefinity 3 to 4.4. Some of this information may become obsolete as newer versions are released. Always watch the official Sitefinity website for the latest info.

As part of the migration of my website from Sitefinity 3, I updated both the Url format for pages (now extensionless) and blog posts (also extensionless and different format) for better readability and SEO. This of course would leave a wake of broken links and search results linking to the old Urls.

There are many options available to resolve this, such as developing a custom HttpHandler to intercept the old Urls. Fortunately, there is a fantastic tool that handles all of this for you.

IIS Url Rewrite

IIS Url Rewrite is an extension for IIS that enables a wealth of Url handling features, including the two I needed for my site: Rewriting and Redirecting.

Redirects

As part of my website update, I moved some of the content such as the Sitefinity 3 Toolkit info to my company website Page Init Solutions. So I need to redirect all these requests from this site to that one.

The Url Rewrite module has an option for creating redirects, which you can manage in a friendly GUI for IIS.

IIS-UrlRewrite

You can also manage this directly in the web.config file. Here are some of the redirects I put in place using the module.

 <rewrite>
            <rules>
                <clear />
                <rule name="Redirect rule1 for PageInit">
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{PageInit:{REQUEST_URI}}" pattern="(.+)" />
                    </conditions>
                    <action type="Redirect" url="{C:1}" appendQueryString="false" />
                </rule>
            </rules>
            <rewriteMaps>
                <rewriteMap name="PageInit">
                    <add key="/software.aspx" value="http://pageinit.net/software" />
                    <add key="/software/Sitefinity/Sitefinity_Toolkit.aspx" value="http://pageinit.net/software/sitefinity-toolkit" />
                    <add key="/software/jQuery/Banner_Swapper.aspx" value="http://pageinit.net/software/jquery-banner-swapper" />
                    <add key="/software/Sitefinity/Sitefinity_Password_Recovery.aspx" value="http://pageinit.net/software/sitefinity-3-password-recovery-tool" />
                    <add key="/blog.aspx" value="http://www.selarom.net/blog" />
                    <add key="/contact.aspx" value="http://www.selarom.net/contact" />
                </rewriteMap>
            </rewriteMaps>
      </rewrite>

This allowed me to easily map old Urls to new, including the old .aspx extensions for pages. Adding a new redirect is as simple as adding a new entry in the list.

Url Rewriting

The new blog Url format makes for SEO friendly as well as functionally useful Urls. They are, however, starkly different from the original format from Sitefinity 3, and clearly, redirecting every single post individually is not an ideal solution.

Fortunately, the Url Rewrite Module also has excellent support for Url rewriting using Regular Expressions. Using Regex, I was able to easily rewrite the old format of /YYYY-MM-DD/Title to the new /YYYY/MM/DD/Title format now used by Sitefinity. I was even able to rewrite some of the urls for the list of blogs by Tag and Category.

Admittedly, working with Regular Expressions is probably one of my weakest programming skills, and despite what everyone keeps saying on Twitter, it does NOT get easier the more you do it! That being said, here are the Regex rewrites I created for this site. If anyone has suggestions for improving or consolodating them, by all means let me know!

 <rule name="BlogPosts" stopProcessing="true">
                    <match url="^(.*)/([0-9]{4})[-]([0-9]{2})[-]([0-9]{2})/(.*)$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="Redirect" url="{R:1}/{R:2}/{R:3}/{R:4}/{R:5}" />
                </rule>
                <rule name="BlogYear2Month">
                    <match url="blog\.aspx$" />
                    <conditions>
                        <add input="{QUERY_STRING}" pattern="Year=([0-9]{4})" />
                        <add input="##{C:1}##_{QUERY_STRING}" pattern="##([^#]+)##_.*Month=(\d{2})" />
                    </conditions>
                    <action type="Redirect" url="blog/{C:1}/{C:2}" appendQueryString="false"/>
                </rule>
                <rule name="BlogYear1Month">
                    <match url="blog\.aspx$" />
                    <conditions>
                        <add input="{QUERY_STRING}" pattern="Year=([0-9]{4})" />
                        <add input="##{C:1}##_{QUERY_STRING}" pattern="##([^#]+)##_.*Month=(\d{1})" />
                    </conditions>
                    <action type="Redirect" url="blog/{C:1}/0{C:2}" appendQueryString="false"/>
                </rule>
                <rule name="BlogYear">
                    <match url="blog\.aspx$" />
                    <conditions>
                        <add input="{QUERY_STRING}" pattern="Year=([0-9]{4})" />
                    </conditions>
                    <action type="Redirect" url="blog/{C:1}" appendQueryString="false"/>
                </rule>
                <rule name="BlogTag">
                    <match url="blog\.aspx$" />
                    <conditions>
                        <add input="{QUERY_STRING}" pattern="tag=(.*)" />
                    </conditions>
                    <action type="Redirect" url="/blog/-in-Tags/Tags/{C:1}" appendQueryString="false"/>
                </rule>

Wrapping Up

The IIS Url Rewrite Module is a fantastic tool for ANY website that has a need for rewriting or maintaining Urls, and is highly recommended for anyone attempting to migrate to the latest verion of Sitefinity.

Finally, here are a few resources I found extremely helpful in preparing these rewrites and redirects:

IIS 7 Adobe PDF Bug Fix

This is just a quick note to document a fix for a horrendous bug in Adobe Reader that causes downloads to fail if they are being served from a Windows Server 2008 machine. It has been plaguing me for weeks, before I even knew it was a bug! The last time I needed help with something I ended up finding the solution on my own blog, so hopefully if I encounter it again, I’ll end up here. Hello future self!

Anyway, the details of the bug are discussed in depth on the iis.net forum, but the basic summary is that a bug in Adobe Reader is causes the file download from IIS to halt, and the user is shown a blank screen. This doesn’t seem to happen if the user SAVES the file then opens it; only opening the file directly seems to cause the error (at least that was my experience).

Anyway, long story short, the fix that worked for me was provided by merk on page 5 of the discussion, which came in the form of an HttpHandler. Basically you map pdf files to run through this handler instead of the standard pipeline, and it does its magic voodoo to properly serve the file to the user.

Merk uploaded his solution to mediafire, you can download it here: http://www.mediafire.com/file/twrd2mnzm3y/PDFHandler2.zip.

I simply dropped it in the bin folder, mapped the handler to intercept *.pdf requests and now all is right with the world.

That’s all for today, with many thanks to Merk wherever you are!