IIS 7 - URL Rewrite - do I include the http or htt

2019-09-09 08:24发布

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?

2条回答
太酷不给撩
2楼-- · 2019-09-09 08:49

@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 "-"

查看更多
何必那么认真
3楼-- · 2019-09-09 08:50

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.

查看更多
登录 后发表回答