Using URL Rewrite Module

Introduction

The Microsoft URL Rewrite Module for IIS 7.0 provides flexible rules-based rewrite engine that can be used to perform broad spectrum of URL manipulation tasks, including, but not limited to:

  • Enabling user friendly and search engine friendly URL with dynamic web applications;
  • Rewriting URL’s based on HTTP headers and server variables;
  • Web site content handling;
  • Controlling access to web site content based on URL segments or request metadata.

Features

The Microsoft URL rewrite module includes these key features:

  • Rules-based URL rewriting engine. Rewrite rules are used to express the logic of what to compare/match the request URL with and what to do if comparison was successful. Web server and site administrators can use rewrite rule sets to define URL rewriting logic.

  • Regular expression pattern matching. Rewrite rules can use ECMA-262 compatible regular expression syntax for pattern matching.

  • Wildcard pattern matching. Rewrite rules can use Wildcard syntax for pattern matching

  • Global and distributed rewrite rules. Global rules are used to define server-wide URL rewriting logic. These rules are defined within applicationHost.config file and they cannot be overridden or disabled on any lower configuration levels. Distributed rules are used to define URL rewriting logic specific to a particular configuration scope. This type of rules can be defined on any configuration level by using web.config files.

  • Access to server variables and http headers. Server variables and HTTP headers provide additional information about current HTTP request. This information can be used to make rewriting decisions or to compose the output URL.

  • Various rule actions. Instead of rewriting a URL, a rule may perform other actions, such as issue an HTTP redirect, abort the request, or send a custom status code to HTTP client.

  • Support for IIS kernel mode and user mode output caching. IIS 7.0 output caching provides significant performance improvements for web applications. URL rewrite module is fully compatible with both types of output caching. This means that it is possible to safely cache responses for rewritten URL's and thus boost the performance of web applications that rely on URL rewriting.

  • String manipulation functions. Built-in string manipulation functions can be used to convert URLs to lowercase and to perform URL encoding and decoding.

  • Rewrite maps. Rewrite map is an arbitrary collection of name-value pairs that can be used within rewrite rules to generate the substitution URL during rewriting. Rewrite maps are particularly useful when you have a large set of rewrite rules, all of which use static strings (i.e. there is no pattern matching used). In those cases, instead of defining a large set of simple rewrite rules, you can put all the mappings between input URL and substitution URL as keys and values into the rewrite map, and then have one rewrite rule which references this rewrite map to look up substitution URL based on the input URL.

  • Failed Request Tracing support. IIS7.0 Failed Request Tracing can be used to troubleshoot errors related to URL rewriting.

  • Rule templates. Rule template is an extension for URL rewrite module user interface, that simplifies creation of rewrite rules for a particular task.  The module includes 3 built-in rule templates, plus it allows plugging in any number of custom templates.

  • UI for testing of regular expression and wildcard patterns. A GUI tool for testing rule patterns is included into the module's user interface. The tool can be used to quickly check how the regular expression or wildcard pattern works. Also, it can be used for troubleshooting and debugging of problems related to pattern matching.

  • UI for managing rewrite rules and rewrite maps. Rewrite rules and rewrite maps can be added, removed and edited by using "URL Rewrite Module" feature in IIS Manager.

  • GUI tool for importing of mod_rewrite rules. URL rewrite module includes a GUI tool for converting rewrite rules from mod_rewrite format into an IIS format.

Where to get the module

Download the x86 version for IIS 7.0

Download the x64 version for IIS 7.0

Upgrading from Go Live release

If you already have Go Live release of URL rewrite module installed then the installation package will upgrade it to RTW release. All rewrite rules in applicationHost.config and web.config files will be preserved.

ASP.NET update

The URL rewrite module installer package includes an ASP.NET update that fixes ASP.NET bugs specific to URL rewriting. In particular, the update contains fixes for the following bugs:

  1. ~ operator in ASP.NET Web server control is resolved incorrectly when using URL rewriting
  2. ASP.NET SiteMap.CurrentNode property returns null when sitemap contains virtual URLs

The update is applied only if the machine where URL Rewrite module is being installed has .NET Framework version 3.5 SP1 or higher. If required version of .NET Framework is installed after URL Rewrite module has been installed, then the ASP.NET update can be applied by re-running URL Rewrite module installer in repair mode.

For more details about these and other ASP.NET issues related to URL rewriting refer to this article.

Using the module

These articles cover the functionality of the URL Rewrite Module and explain how to use it to accomplish common URL rewriting scenarios.

Learning the basics

References and guidance

Video walkthrough

Changes since Go Live release

The following additions, changes and important bug fixes have been made to URL rewrite module since Go Live release:

  1. Added string manipulation functions UrlEncode and UrlDecode.
  2. Provided access to a raw original URL via server variable UNENCODED_URL. This server variable now contains the raw encoded URL exactly as it was requested by web browser.
  3. Added UI for configuring rewrite rules for Application Request Routing.
  4. Added support for in-place upgrade from Go Live release of URL rewrite module.
  5. Added functionality to clean up the internal cache to avoid excessive usage of memory when caching rewrite rules.
  6. Fixed a bug related to rewriting of URLs that contain non-ASCII characters.
  7. Fixed a bug in regular expression engine that caused character ranges to be evaluated incorrectly for case insensitive patterns.
  8. Included an update for IIS FastCGI module into the installer package.
  9. Included an update for System.Web.dll into the installer package.
  10. Included an update for IIS SetUri function into the installer package.

Known Issues

  1. mod_rewrite rules that use REQUEST_URI server variables may not work correctly when imported by using URL Rewrite “Import Rules…” feature. To fix the imported rules use any text editor to open the web.config file that contains those rules. Locate the <rewrite> section and then within that section replace all instances of string “{SCRIPT_NAME}” with the string “{URL}”. 
  2. Digest authentication cannot be used with URL rewrite module.
  3. ASP.NET Forms Authentication uses rewritten URL for redirection. For example, if requested URL is "/article.htm" and URL rewrite module rewrites the URL to "/article.aspx", which is protected by Forms Authentication, then ASP.NET will redirect to "/login.aspx?ReturnUrl=%2Farticle.aspx".
  4. Auto-detect mode in ASP.NET Forms Authentication uses rewritten URL for redirection. For example, if requested URL is "/article.htm" and URL rewrite module rewrites the URL to "/article.aspx", which is protected by Forms Authentication, then ASP.NET will redirect to "/article.aspx?AspxAutoDetectCookieSupport=1".
  5. UseUri mode in ASP.NET Forms Authentication uses rewritten URL for redirection. For example, if requested URL is "/article.htm" and URL rewrite module rewrites the URL to "/article.aspx", which is protected by Forms Authentication, then ASP.NET will redirect to "/(S(vy2ebt45imfkmjjwboow3l55 ))/article.aspx".
  6. ASP.NET rewrites back to the original URL when using URI-based authentication or cookie-less session state. For example, when request is made to "/(S(vy2ebt45imfkmjjwboow3l55 ))/article.htm" and URL rewrite module rewrites "/article.htm" to "/article.aspx", then ASP.NET will rewrite the URL back to "/article.htm", which may result in a "404 - File Not Found" error.
  7. IIS DefaultDocument module may redirect to rewritten URL when URL rewrite module rewrites to a folder. For example, if substitution URL in rewrite rule is "/folder1/folder2" and these folders exist on file system and have default document configured, then DefaultDocument module will redirect web client to "/folder1/folder2/", thus exposing rewritten URL. To prevent this from happening, use "/" at the end of the substitution URL when rewriting to a folder, e.g. "/folder1/folder2/"

Related Content

Comments

I'm curious if you'll implement functionality to 'redirect POSTs' , too?

I've see plenty of instances where folks want to 'redirect' all traffic to a site, be it a GET or a POST. GETs work fine w/ a standard redirect (the querystring is easily appended to the new Location), but POSTs do not. That is, if a POST is redirected, the browser does not re-POST to the new location; instead it issues a GET to the new Location, but the body of the POST data is lost. The loss of the data can -- in certain instances -- be problematic.

The solution to redirecting a POST is pretty simple ... instead of issuing a HTTP redirect, send back a hidden HTML form to the browser with all of the name/value POST data as hidden fields, and, via client side scripting (onLoad) have the form auto-submit a new POST to the new 'redirected' URL.

This isn't a true redirect, but serves the same end folks often want.

Bottom-line: I think it would be valuable to bake this POST psuedo-redirect functionality into the rewrite module. Perhaps when a user selects to do a redirect, there could be a checkbox to 'Also perform scripted redirect for POSTs' (or something better worded) which would enable this behavior.

Jun 12 2008 by geekinabox

Что же ты делаешь с нами великий и могучий ... IIS 7 ?
http://dev.net.ua/blogs/leshchinsky/archive/2008/06/17/6339.aspx

Jun 17 2008 by Leshchinsky

It's great that Microsoft is providing an official module for this - there's just one very important thing missing for me:

There's no way to extend it, for example, to read/write the customer's cookies. I would like to change the URL depending on a cookie from the customer, as well as update their cookie in certain cases. Is there anything planned for this?

Jun 24 2008 by lonevvolf

Hmmm, well, reading cookies seems to work using {HTTP_COOKIE}, but I don't think writing to the response will work. In any case, I can't yet seem to select a single cookie value with a regular expression. :S

Jun 24 2008 by lonevvolf

Lonevvolf, you are right about how to read a cookie. The cookie can be accessed by using {HTTP_COOKIE} as an input in rule's condition.

If you cannot get the regex pattern to match the cookie value, as a first step I would recommend to enable failed request tracing (http://learn.iis.net/page.aspx/467/using-failed-request-tracing-to-trace-rewrite-rules/) and then see what the {HTTP_COOKIE} value actually contains. That way you will have a better idea about how the cookie string looks like and may be able to adjust your regex accordingly.

There is no functionality in URL rewrite module to set the cookie for the response.

Jul 01 2008 by ruslany

Hi. Can the IIS7 URL rewriter be used with Cassini server? I imagine not, but I wonder how to incorporate this useful feature into the development process. The Intelligencia offering allows development with Cassini and then deployment to a web server. It would be great to use the new IIS7 version instead.
Thanks!

Jul 18 2008 by riggit

Ruslany, when I try to install the URL Rewrite Module on my Windows Vista SP1 (with IIS 7.0) PC for testing, I get the following error: IIS Version 7.0 or greater is required to install Microsoft URL Rewrite Module for IIS 7.0.

Any ideas how to get around this problem? Thanks.

Jul 18 2008 by mwoj

Possible way to workaround the error reported by mwoj is to run the installer from the command line:

msiexec /I rewrite_x86_TP1.msi

Jul 21 2008 by ruslany

Hello,

Take an example scenario URI:
"index/goto/www/return/%2Ftickets%2Fcategory"

The rewrite module states that it contains the original in the X_ORIGINAL_URL, but the output from that is actually:
"index/goto/www/return/tickets/category"

Any idea on what could cause this?

Thanks!

Oct 27 2008 by kevin.peno

Hi, a quick question for the forum:

URL Rewrite Module for IIS 7.0 documentation states it is not a reverse-proxy, from the chaching point of view, but I am very interested to find out if it can serve an array of servers and not a single machine.

I am trying to build the following architecture to secure the provision of a Web application:

User's browser approaches server1 (IIS7 win 2008 64bit) with the URL https://server1/directory1/page1.php. Besides its connection to the Internet, server1 is also connncted to an internal LAN, and I want to rewrite the URL to into http://server2/directory2/page1.php. This is done to provide a layer of security by not exposing the name of the internal server2.

Would appreciate your advice, thanks, Jonas.

Feb 06 2009 by lonas_halor

Ohh! Thanks. Long time I finding info about URL Rewrite.
I follow to your site from codex.wordpress.org. Thangs again.

Mar 24 2009 by thailandseo

I have a question about using the Rewrite Module. First off, let me say - rock on - this thing is amazing in what it can do.

Here's the scenario:
All sub1.domain.com/whatever/file.aspx goes to:
sub2.domain.com/whatever.file.aspx

The trick I want is a nice simple rule that says "if it was HTTPS, send to HTTPS. If it was HTTP, send to sub2 as HTTP"

What's a good regex to catch it. For now, I'm just using ^(.*)$ and sending everything to plain HTTP, non SSL. I'd like for it to persist the SSL/HTTPS or non, if I could.

May 09 2009 by Jason Gross

Greetings Net,

When running the installer: rewrite_1.1_x86_rtw.msi
I get the following error message:

This installation package cannot be installed by the Windows Installer service. You must install a Windows service pack that contains a newer version of the Windows Installer service.

I have checked and I'm fully up-to-date on Windows Update.

I'm running Windows Server 2003 Web Edition, SP2.

Can anyone point me to the service pack which I need to install so that I can install URL Rewrite?

May 27 2009 by jhms09

Jason Gross, check out this post - it should answers your question about HTTP vs HTTPS: http://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/

Jun 06 2009 by ruslany

jhms09, URL Rewrite Module cannot be installed on Windows Server 2003. You have to have Vista, Windows Server 2008 or Windows 7 to install it. It works only with IIS 7.

Jun 06 2009 by ruslany

TO ALL: PLEASE POST ALL YOUR QUESTIONS ABOUT THE MODULE HERE: http://forums.iis.net/1152.aspx. YOU WILL GET MUCH FASTER RESPONSE!

Jun 06 2009 by ruslany

Guys. I use this module on my site and found out strange stuff.
It doesn't work with simbol ":" in file name.
I have original link like that "products.aspx?id=A11&coment=blablabla"
and i change that new "A11-blablabla-products.html"
and this module change it back and it works.

BUT

If in that new link exist simbol ":" or html code of it "%3a" it doesn't work.
This is an example of those problems.
"A11-bla:bla-products.html"
"A11-blabla%3a-products.html"

I use this pttern to change it "([\w]+)-([\w\%\:]+)-products.html"
and in debug tool it work, but when i open it with IIS it said "page not found".

I think it happend because IIS don't allow use ":" in file name,
but this if fake file and i don't need that restriction.
How to fix that problem?

Jun 10 2009 by DukeNuken

DukeNuken, try configuring IIS Request Filter to allow double encoding. Place this xml into the web.config file inside of the configuration>/system.webServer>/ element:

security>
requestFiltering allowDoubleEscaping="true" />
/security>

Aug 07 2009 by ruslany

Is it possible to store the rules in a SQL Server database rather than in the web.conifg?

Aug 08 2009 by richierich112582

Hello!
Actually, in version 2 (beta) of the Url rewrite Module there is still some bugs around rewrite and postback...
To avoid POSTBACK and rewrite issues you should add the following code into the Page_load:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
MyFormID.Action = Request.RawUrl
'[...your code ...]
End Sub

Have fun ;)

Aug 10 2009 by Cavone.com

Hi!

How can I use named groups in rx pattern? Couldn't find it in documentation..

Oct 15 2009 by andr

I am facing URL rewite problem at godaddy server... All hour suggestion are good but couls not help when I try to configure at godday server
thanks
arun

Nov 04 2009 by xponsewebs

Hi,

I am running my site http://www.bridepower.com on IIS 7 and have implemented IIS Rewrite module.

I am in need of having http://www.bridepower.com/default.aspx redirect to http://www.bridepower.com and it does that as well.

But the MAJOR issue is when I implement redirect and user press a button on homepage which leads to a postback it again sends user to http://www.bridepower.com and the event does not call up. So I had to remove it.

Any luck to anyone in this area?

Dec 15 2009 by jigneshthanki

Is there any ETA on a fix for issue #7?

"7.IIS DefaultDocument module may redirect to rewritten URL when URL rewrite module rewrites to a folder."

My domain is on a shared server and the module redirects to my root. However since I have multiple domains hosted in sub folders, this issue actually causes the parent directory path from the subfolder to the root to be exposed on the URL.

Meaning is IIS.net was my domain and it was located in a "iisroot" folder then www.iis.net/downloads would redirect to www.iis.net/iisroot/downloads/ which seems like a major issue as a website should never be exposing it's parent directory.

Dec 15 2009 by TRSully

a href="http://radenbeletz.com">Raden Beletz/a>
hi, i'll change my blog platform from wordpress to asp.net. what can i do that ?

Dec 20 2009 by radenbeletz

pls help me, what can i do for change cms from wordpress wo asp.net. this my site http://radenbeletz.com

Dec 20 2009 by radenbeletz

I cannot figure out how to require the www prefix or add it into a url. I need to have all http://somedomain.com redirected to http://www.somedomain.com. I'm assuming this is possible with this. can somebody help me out?

Jan 28 2010 by NorthlandSticks

This isn't a true redirect, but serves the same end folks often want.


http://www.vienka.com

Jan 31 2010 by piyenk

Hi,

I am running my site http://www.bridepower.com on IIS 7 and have implemented IIS Rewrite module.

I am in need of having http://www.bridepower.com/default.aspx redirect to http://www.bridepower.com and it does that as well.

But the MAJOR issue is when I implement redirect and user press a button on homepage which leads to a postback it again sends user to http://www.bridepower.com and the event does not call up. So I had to remove it.

Any luck to anyone in this area?

Dec 15 2009 by jigneshthanki

------------------------------------
I got the same problem as you. Does any IIS7 experts here know the solution? Thanks.



Feb 05 2010 by pfsze

Hi,

I am running my site http://www.bridepower.com on IIS 7 and have implemented IIS Rewrite module.

I am in need of having http://www.bridepower.com/default.aspx redirect to http://www.bridepower.com and it does that as well.

But the MAJOR issue is when I implement redirect and user press a button on homepage which leads to a postback it again sends user to http://www.bridepower.com and the event does not call up. So I had to remove it.

Any luck to anyone in this area?

Dec 15 2009 by jigneshthanki

------------------------------------
I got the same problem as you. Does any IIS7 expert here know the solution? Thanks.

-pfsze

-----------------------------------
I just found a solution for this issue. God bless.

in index.aspx page, add a PostBackUrl behavior to the button:
...
form id="form1" runat="server" method="post">

div>

asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" PostBackUrl="~/the_redirected_page.aspx" />

/div>
/form>
...

Hope my simple solution will help you. :)





Feb 06 2010 by pfsze

Hi,

I am running my site http://www.bridepower.com on IIS 7 and have implemented IIS Rewrite module.

I am in need of having http://www.bridepower.com/default.aspx redirect to http://www.bridepower.com and it does that as well.

But the MAJOR issue is when I implement redirect and user press a button on homepage which leads to a postback it again sends user to http://www.bridepower.com and the event does not call up. So I had to remove it.

Any luck to anyone in this area?

Dec 15 2009 by jigneshthanki

------------------------------------
In order to make the button event run,
1> you need to set the index.aspx as your default document on top in IIS7 server instead of default.aspx.
2>You add a rule to redirect http://domain/default.aspx to your domain name http://domain/
3>Then, you copy all contents over from default.aspx to index.aspx. After all, the button event should run on your http://domain/

In my case, the button onclick event will submit input data to another page, so index.aspx will never show up in my website. I am not sure what button event will proceed in your case. Anyway, give it a try.

Feb 07 2010 by pfsze

I am having a small problem using the url rewrite feature. I am trying to setup a web page with one url, but that accesses two different folders based on the information after the / for example we could have foo.edu/staff or foo.edu/student_gradyear. Then on the server there would be a staff folder and a student folder with all the user folders located within them, so www\staff\user and www\student\user. I seem to have this working correctly, accept for the fact that if you just go to the address foo.edu/student_gradyear instead of foo.edu/student_gradyear/index.html it will not use the default document types and the page won't load. Any ideas on how to fix this problem?

Feb 08 2010 by moondogelivs007

It's really very use article.Your post help me to clear some of my queries about this service.

Thanks
a href="http://www.medxcycle.com/" title="Keep Healthy">Keep Healthy/a>
a href="http://www.myzimzim.com/" title="Travel Site">Travel Site/a>

Feb 08 2010 by M4laa

Thank very much!

I'm using URL Rewrite module with http://www.usmarketdb.com and http://www.kadnad.com it's was working!

But when i implement with wordpress 2.7 with http://www.motorcycles-reviews.com it not working. It still using /index.php/story-zzzz somthing like that

Thank you for your kindly suggestion,
http://www.adsfreepost.com

Feb 09 2010 by kasapa

Submit a Comment

You must Log In to comment.