URL Rewrite And A Sorry Server

by Vaughn Anderson 2. August 2011 10:00

We have our load balancers configured if all the servers in a VIP fail, then they redirect all traffic to a sorry server.  This seemed easy enough, but then when we tested it, we found some weird results.  For example, if someone goes to http://www.site.com and that request hits the sorry server, things work without any issues.  The sorry server will display the under maintenance page since the user was trying to access the default document on the web site.

What if a user has a login page bookmarked and it looks something like this – https://site.com/login.aspx?ID=725?  That request will not be captured by the default document.  Initially, I thought I could just make my “Under Maintenance” page my default 404 error page displaying the under maintenance banner, but that’s bad hosting.  The proper response for something that is temporarily unavailable is a 307.  Therefore, I needed a way to redirect all traffic to the maintenance page, but give a 307 response.  Plus, it must be a lightweight solution since the sorry server is a VM that does not have that much processing power.  URL Rewrite to the rescue.

Open URL Rewrite in IIS and create a new Blank Rule.  I called mine “Redirect all.”  Here’s a picture of my configuration and the code if you want to add it directly to your web.config.  Then I will explain why I configured it this way.


Web.config code:

    <rule name="Redirect all" enabled="true" stopProcessing="true">
        <match url="(.*)" negate="false" />
        <action type="Redirect" url="/index.htm" redirectType="Temporary" />
            <add input="{REQUEST_FILENAME}" pattern="index\.htm" negate="true" />

So let’s break this down section by section.

  • In the Match URL area, I want to capture all traffic, hence the (.*) pattern.
  • For the Conditions, I wanted anything that did not match the default document of index.htm to flag a condition. This is because the only page that works properly is the default document.
  • I didn’t have any server variables.
  • For the Action, I wanted to redirect all traffic caught in the “Conditions” section and redirect it to the default document.  Since I have many different URLs feeding to this rule, I just left the generic relative path of /index.htm.


IIS | URL Rewrite

SEO Compliance

by Vaughn Anderson 11. July 2011 15:06

Search engine optimization (SEO) is a pain, but if you want to maintain your high rankings as a company, you have to comply by the sometimes confusing standards.  We recently updated our company web site and had to do several things to make sure the new web site did not drop our search engine rankings.

The first item I had to fix was all of the old common pages needed to have a 301 redirect pointing to the new page.  Since our old site was written in ASP, this was not as simple as adding some redirects to the web.config and being done.  I had to create the redirect rule for each of the old pages and add them to our web site.  Obviously, I did not do this for every old page, just the important ones.  To accomplish the redirect  I created a file with the exact same path and file name as the old file.  Then, I added the following code:

<%@ Language=VBScript %>
Response.Status="301 Moved Permanently"
Response.AddHeader "Location",”http://www.site.com/newpage.aspx”

Basically the only litem you need to change is the “http://www.site.com/newpage.aspx” entry to point to your new page.

Once we had fixed the old pages that would have resulted in a 404 error, we had to fix some other common issues.  To do this, I used URL Rewrite, a free add-on to IIS 7.  You can use the GUI for URL Rewrite in the IIS 7 interface, but you can also just copy rules directly into your web.config file.  All you need to do is create a section called <rewrite> in the <system.webServer> section.  Then, each rule starts with a <rule name=”…> section.  I am going to be giving my examples through additions to the web.config file and not using the GUI.

Scenario: Web crawler sees http://site.com different from http://www.site.com
Solution: Rewrite the URL by using the following rule

<rule name="WWW redirect" enabled="true" stopProcessing="true">
    <match url="(.*)" />
        <add input="{HTTP_HOST}" pattern="^www\.site\.com$" negate="true" />
    <action type="Redirect" url="http://www.site.com/{R:1}" />

Scenario: SEO and the default document do not agree on the way to display text.  For instance, http://www.site.com and http://www.site.com/default.aspx are the same page, but SEO results do not like that.
Solution: Rewrite the URL so it either always displays the /default.aspx or never displays it.  I opted to have it never show just because I think it looks cleaner.

<rule name="Default Document URL Rewrite" enabled="true" stopProcessing="true">
    <match url="(.*?)/?Default\.aspx$" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
    <action type="Redirect" url="{R:1}/" />

Scenario: SEO sees http://www.site.com different from http://www.site.com/
Solution: Rewrite the URL so it always includes the trailing slash.

<rule name="Trailing Slash" stopProcessing="true">
    <match url="(.*[^/])$" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{URL}" pattern="WebResource.axd" negate="true" />
    <action type="Redirect" url="{R:1}/" />

Scenario: SEO sees http://WwW.SiTe.CoM different from http://www.site.com|
Solution: Eliminate uppercase letters in your URLs

<rule name="Convert to lower case" stopProcessing="true">
     <match url=".*[A-Z].*" ignoreCase="false" />
     <action type="Redirect" url="{ToLower:{R:0}}" redirectType="Permanent" />

Hope that helps.  Also, the rules are processed in the way they are processed in the web.config file, top to bottom, so arrange the rules in the order you want them processed.


IIS | SEO | URL Rewrite

URL Rewrite Exclusions

by Vaughn Anderson 21. March 2011 11:29

We are reviewing our current setup in IIS at work.  Our company site is still written in ASP so we were doing the redirect to www.domain.com through a 302 redirect in IIS by having a site dedicated to doing the redirect.  This works ok, but I prefer to not have two sites in IIS when one site will do.  Therefore, I downloaded URL Rewrite and configured it handle the redirect.  This was easy. Here’s a picture of what I configured (click on image for full size picture).


The highlights are as follows:
Match URL Section
Requested URL: – Matches the Pattern
Using: – Regular Expressions
Pattern: – (.*)
Input – {HTTP_HOST}
Type – Does Not Match the Pattern
Pattern - ^www\.domain\.com$
Action type – Redirect
Action Properties (Redirect URL)
Append query string – checked
Redirect type: – Found (302)

The problem came when we needed to exclude secure.domain.com.  Since the redirect was redirecting all traffic that did not match the expression www.domain.com, if you came in through secure.domain.com, you would get redirected to www.domain.com. This required us to add a new rule.  Here are the highlights for this rule.


The only two changes were in the Conditions area, we wanted to MATCH the pattern and then in the action, set the action type to None and then check the box to stop processing all subsequent rules.


Lastly, set the order of the rules so the secure.domain.com rule is first and the redirect rule is second.  This will allow the redirect rule to be processed second.  When the secure.domain.com rule process first, it enables it to exclude the redirect rule from processing.



IIS | IIS7 | URL Rewrite

About the author

Just a guy trying to record what he does in IIS and Hyper-V so others might benefit.

Month List