IIS 7 Canonical URL redirect

丶灬走出姿态 提交于 2019-12-04 04:58:12

You're right that the full URL needs to be in the web.config. You have options though.

  1. You can use a config transform to make the regular expression match the correct environment.

  2. There doesn't seem to be any harm if you include all three URL rewrite rules in your web.config. It sounds like your environments are isolated so each environment would only ever match one of the rules. That can clutter your web.config, but not horribly.

I'd go with option 1. You can find information on config transforms here: http://msdn.microsoft.com/en-us/library/dd465326.aspx

I have another solution for you:

<rule name="Canonical domain name" enabled="true" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
        <add input="{HTTP_HOST}" negate="true" pattern="^www\.([.a-zA-Z0-9]+)$" />
    </conditions>
    <action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>

I would also suggest you another variant for testing in local environment:

  1. Add to c:\Windows\System32\drivers\etc\hosts:

127.0.0.1 www.example.com

  1. In IIS Manager select site and right click -> Edit bindings.. -> Add..

  2. Enter host name: www.example.com

  3. Open cmd and run iisreset

Now you are able to use www.example.com in browser which is mapped to localhost

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