Click here to Skip to main content
13,793,791 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 16 Aug 2016
Licenced CPOL

Easy Way to Fix Outdated Links (URL Rewrite Rule in Web.config)

, 16 Aug 2016
Rate this:
Please Sign up or sign in to vote.
Easy way to fix outdated links (URL rewrite rule in web.config)


I’ve recently moved my site from BlogEngine.NET 2.0 to 3.3 – thanks to the great work done by BlogEngine.NET team, the migration was easy. The only serious problem I’ve noticed was with post links ending with .aspx. For example, when Google or CodeProject had a link to such URL:

the post was not found. If .aspx suffix was removed from the address: 

everything was working fine! Fortunately, fixing it didn’t require any BlogEngine code changes – all thanks to URL Rewrite Module 2.0 (available since IIS 7) and system system.webServer/rewrite Web.config section.

URL Rewrite is a big topic. Checkout docs if you want to know all the details – you can even do things like setting HTTP headers or server variables! In this post, I will focus on how to fix the .aspx link problem and I will also note some issues you might face while trying to setup your own URL rewrite rules…

Setting the Rules

I’ve added such rewrite section inside system.webServer node in Web.config file:

        <rule name="FixOldAspxLinks" stopProcessing="true">
            <match url="^(.*post/.+)\.aspx$" />
            <action type="Redirect" url="{R:1}" redirectType="Permanent" />

It has a single rule that matches all addresses that contain post/ and end with .aspx and triggers redirect action to the same address but with .aspx part dropped.

The Rule

Rule has a name (something describing the purpose of the rule is welcome) and stopProcessing=”true” setting which instructs IIS to skip any further rules for matched URL (yes, there's only one rule but having stopProcessing=”true” makes our intention clear).

The Match

If you are familiar with regular expressions the url="^(.*post/.+)\.aspx$" attribute should be obvious, if not - don’t worry, it’s simpler than it looks:

  • ^ – means beginning of URL
  • $ – means the end of URL
  • .* – means any character zero or more times
  • .+ – means any character at least once
  • \. – means a literal dot (in regexes . stands for any character so if we literally want to look for a dot, we need to escape the special meaning by preceding it with backslash)
  • () – parentheses denote the text (capturing group) what we are going to reference in action element by using {R:1}

The matching expression could be written in many ways but the one I’ve used solves the problem without going overboard with URL pattern recognition…

The Action

We want the browser to look for a new address hence type="Redirect" is set.
New address is specified with url="{R:1}". The {R:1} is a reference to the group captured by matching expression – its value is the text found between parentheses. In our case, it’s everything that preceded the .aspx suffix. redirectType="Permanent" instructs the server to issue a 301 Moved Permanently response to the browser. When HTTP client receives permanent redirect, it will use the new URL each time it sees a link to the old URL…

Ok, so the above rewrite should be all that’s needed to make .apsx problem disappear! Doesn’t work on your machine? Read on!

Possible Issues

No URL Rewrite Module Installed

Before pushing any changes to remote server, I wanted to check rewrite settings on my local IIS 7.5 on Windows 7 x64. I did it and instead of redirect, I got HTTP Error 500.19 – Internal Server Error. The error page was useless as it didn’t show any hint on what was wrong with the config... If you face the same issue, you probably don’t have IIS Rewrite module installed (it is not added by default). Quick way to find out if you have the module is to check if this file exists:


I got the installer from here: After module was added to IIS, the rewrite rule started to work. :)

Redirect Caching

Rewrite rule is setup to redirectType="Permanent" because we want to teach HTTP clients that the resource is moved for good, right? It's all ok unless you are doing development and make some changes to the rule – if browser already received 301 response for particular URL, your modified rule will not get a chance to work! To solve this problem, you can clear the cache but I prefer to have Chrome's dev tools open (with caching disabled) or try to open the page in fresh incognito window…

Pattern Testing

Regular expressions are a powerful tool, but it's very easy to make a mistake while working with them. Fortunately, IIS Rewrite Module has its own panel (snap-in) in IIS Manager:

URL Rewrite module in IIS Manager... Click to enlarge...

that lists rewrite rules used for the site:

Rewrite rule in IIS Manager... Click to enlarge...

If you double click a rule, you will see a window that lets you change rule properties without manual modifications to Web.config. Pressing "Test pattern..." button opens the window in which you can quickly test your regular expression:

Pattern test in IIS Manager... Click to enlarge...


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

You may also be interested in...

Comments and Discussions

-- No messages could be retrieved (timeout) --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.181207.3 | Last Updated 16 Aug 2016
Article Copyright 2016 by morzel
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid