URL Rewrite Rules for DasBlog Month and Date Pages

September 20th 2015 DasBlog IIS

Redirecting old URLs to their new counterparts is an important part of restructuring an existing web site or migrating it to a new engine. In a previous blog post I've already described, how I kept all permalinks working after the migration. I took a similar approach for mapping old categories to new tags; with the only difference that I had to define the mappings manually because of the content reorganization I did at the same time.

I still had to correctly redirect the old month and date pages. In contrast to all previously mentioned sets of pages, I didn't want to create full rewrite maps for these. There are just too many of them for such an approach; creating a more generic rule makes much more sense in this case.

Since month index URLs contain English month names in my new site, I first had to map month numbers to these names. A rewrite map is still the best way for doing it, therefore I added another one to my existing rewriteMaps.config file:

<rewriteMap name="months" defaultValue="">
  <add key="01" value="January"/>
  <add key="02" value="February"/>
  <add key="03" value="March"/>
  <add key="04" value="April"/>
  <add key="05" value="May"/>
  <add key="06" value="June"/>
  <add key="07" value="July"/>
  <add key="08" value="August"/>
  <add key="09" value="September"/>
  <add key="10" value="October"/>
  <add key="11" value="November"/>
  <add key="12" value="December"/>

Unlike all other rewrite maps, this one was going to be used for only a part of the final full URL. Here's an example of an old URL I had to redirect:


And this is what I had to redirect it to:


As you can see, there were two dynamic parts in the URL:

  • the year which could be copied verbatim,
  • the month which needed to be transformed using the months rewrite map.

Fortunately IIS URL rewrite module is flexible enough to support this:

<rule name="monthLinks">
    <match url="^default,month,([0-9]+)-([0-9]+)\.aspx$" />
        <add input="{months:{R:2}}" pattern="(.+)" />
    <action type="Redirect" 
            redirectType="Permanent" />

The rule probably warrants some explanation:

  • The regular expression in match url attribute matches the old URLs and defines two groups: one for year and one for month.
  • The only condition applies the months rewrite map to the month (second) group ({R:2}) from the regular expression.
  • The final URL includes both the mapped month name from the condition ({C:1}) and the unchanged year (first) group from the regular expression ({R:1})

A similar approach worked for redirecting old date URLs, as well. Because the new site doesn't have date indexes, I just redirect them to month pages. The visitors will not get the exact same content, but they should be able to easily navigate to the content they're looking for, nevertheless. Here's my rule:

<rule name="dateLinks">
    <match url="^default,date,([0-9]+)-([0-9]+)-([0-9]+)\.aspx$" />
        <add input="{months:{R:2}}" pattern="(.+)" />
    <action type="Redirect" 
            redirectType="Permanent" />

Nothing new in this rule: there are 3 groups in the match regular expression instead of only two; I just ignore the last one, representing the day. The rest of the rule is identical to the one for month pages.

To learn more about back references, which I used in the rules above, check URL rewrite module reference documentation.

Get notified when a new blog post is published (usually every Friday):

If you're looking for online one-on-one mentorship on a related topic, you can find me on Codementor.
If you need a team of experienced software engineers to help you with a project, contact us at Razum.
Creative Commons License