TwitterLinkedInBlog

Thursday, November 10, 2011

Using URL Rewrite to Redirect

The following is performed at the web site level in IIS. See 'Resources' below for the 'URL Rewrite' download link, the user reference guide, and a couple other helpful links.
Match URL > Requested URL: 'Matches the Pattern' > Using: 'Regular Expressions' > Pattern:

*Note: Pattern: tests against the <path>, not the entire URL!
http(s)://<host>:<port>/<path>?<querystring>

So if you want to test if a string is in the beggining of the path, you would use ^. For example, if you want to redirect...
http://intranet.samplecompany.com/hr/Pages/default.aspx
... to ...
http://intranet.samplecompany.com/teams/hr/Pages/default.aspx
... you would use:
Pattern: ^hr/.*

Then, Action > Action type: 'Redirect' > Action Properties > Redirect URL:
http://intranet.samplecompany.com/teams/{R:0}
The {R:0} is a back reference variable. You can see it when you use the 'Test pattern...' button in the 'Match URL' section of the form.

*Tip: When using the 'Test pattern...' button > Input data to test:
Use the path, not the entire URL! Remember, the pattern tests against the path, not the entire URL!

*Note: It seems that when using 'Conditions', the path needs a leading forward slash. So for the 'Redirect URL' with no Conditions, one would use the pattern...
^hr/.*
... and the 'Redirect URL'...
http://intranet.samplecompany.com/teams/{R:0}

... but with Conditions, one would use the Condition pattern...
^/hr/.*
... and the 'Redirect URL'...
http://intranet.samplecompany.com/teams{C:0}

*Another Note:
If you attempt to browse to a non-existent page, you will get a 404 error. This is by design, because if the following link doesn't exist here...
http://intranet.samplecompany.com/hr/Pages/sample.aspx

... it's not going to exist here either...
http://intranet.samplecompany.com/teams/hr/Pages/sample.aspx


Resources:

No comments: