IIS 7 - URL Rewrite - do I include the http or https protocol in the regex?

空扰寡人 提交于 2019-12-13 20:38:17

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/14468014/iis-7-url-rewrite-do-i-include-the-http-or-https-protocol-in-the-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!