正则表达式URL匹配除www以外的任何内容
问题描述:
我正在使用IIS7和URL重写模块.
I'm using IIS7 and the URL Rewrite module.
我想使用正则表达式来匹配www以外的任何子域.
I would like to use regex to match any subdomain apart from www.
所以...
frog.domain.co.uk = Match
m.domain.co.uk = Match
anything.domain.co.uk = Match
www.domain.co.uk = No match
通过这种方式,我可以将有人输入的任何子域重定向回www.
This way I can redirect any subdomain that someone types in back to www.
答
使用此规则-如果域不同,它将重定向到www.exmaple.com
域:
Use this rule -- it will redirect to www.exmaple.com
domain if domain is different:
<system.webServer>
<rewrite>
<rules>
<rule name="Force www" stopProcessing="true">
<match url="(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.example\.com" negate="true" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
如果您不想两次输入域名(example.com
),则可以对其进行一些优化-但这是非常小的事情,根据您的情况/配置,这可能是不希望的.
You can optimize it a bit if you do not want to type domain name twice (example.com
) -- but that is very minor thing and depending on your circumstances/configuration it is can be undesired.