In IIS 7.5 (Win2k8 R2) I'm trying to create a new rule for these requests:
http://www.domain.com/userprofile
https://www.domain.com/userprofile
http://domain.com/userprofile
https://domain.com/userprofile
rewriting to:
http://www.domain.com/users/?username=userprofile
(or whatever the protocol/domain is)
The regex that I wrote is:
^(http|https)://(www\.domain.com|domain\.com)/([a-zA-Z0-9-]{6,35})
and the rewrite is:
{R:1}://{R:2}/users/?username={R:3}
But this is not working. Is it because I don't need to the protocol? I also added conditions that the request is not a file or directory.
Also, do I need to restart IIS each time I change the rule?
You don't have to look at the protocol or domain name when you want to rewrite these request. It's not important in your case as you only want to rewrite the path.
The following rule should work:
<rule name="Rewrite user profiles">
<match url="([a-zA-Z0-9-]{6,35})" />
<action type="Rewrite" url="/users/?username={R:1}" />
</rule>
You don't have to restart IIS when you change the rule. IIS will automatically restart the application pool when web.config is modified and hence reload the rules.
@Marco was almost right, but here's how what ended up working: (I used the URL Rewrite form in IIS Manager)
Regex:
^([a-zA-Z0-9-]{6,35})(/?)$
Conditions:
Not a file
Not a directory
This forces the match to start directly after the domain and must be either the first directory, or with no trailing "/". According to the regex, the match must be between 6 and 35 characters, alpha numeric with "-"