IIS Host Headers and non WWW to WWW

こ雲淡風輕ζ 提交于 2019-12-02 19:35:49

问题


I know there's a bunch of examples on how to redirect your non www to your www site, but I'm not using any rewrite utils/ISAPI.

On my Windows 2008R2 box, I have several sites setup in IIS. I setup host headers for both www and non www versions. The first couple of sites work fine. If you try to go to the non www site, you are automatically redirected to the www version.

As far as I recall, I didn't have to do anything special other than add the appropriate host headers - no messing around with rewrites/ISAPI.

What am I missing on the server manager side of things in order to get this working?


回答1:


I guess there are two ways. One is to create a rewrite rule through the IIS manager.

The other is to setup the system.webserver section of the web.config as follows:

  <system.webServer>

    <rewrite>
      <rules>
        <clear/>
        <rule name="Redirect Non WWW to WWW" 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>

        <!--<rule name="Default Document" stopProcessing="false">
          <match url="(.*)default.aspx"/>
          <action type="Redirect" url="{R:1}" redirectType="Permanent"/>
        </rule>-->

      </rules>
    </rewrite>

    <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true"/>

    <httpErrors errorMode="Custom"/>

  </system.webServer>


来源:https://stackoverflow.com/questions/8440312/iis-host-headers-and-non-www-to-www

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