简体   繁体   中英

Redirect to www in IIS7 - classic pipeline mode

I want to implement redirects on an IIS7 webserver. Basically, if the subdomain is not included in the URL, I will redirect to the www subdomain.

http://mysite.com/file.aspx redirects to http://www.mysite.com/file.aspx

http://mysite.com/image.jpg redirects to http://www.mysite.com/image.jpg

http://mysite.com/text.html redirects to http://www.mysite.com/text.html

How to do this?

I do not want to write any HTTP Module -- it must be done thru IIS config only.

Also, I am required to use Classic Pipeline mode and cannot install any ISAPI plugins.

Is it possible?

You can throw this into your web.config file:

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Redirect to WWW" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^http://mysite.com$" />
          </conditions>
          <action type="Redirect" url="http://www.mysite.com/{R:0}" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

In IIS7 it can be done through the url rewrite section.

This solution worked for me:

1) Install URL Rewrite component:

http://www.iis.net/download/urlrewrite

2) Add to web.config:

<system.webServer>
 <rewrite>
  <rules>
   <rule name="CanonicalHostNameRule1" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
     <add input="{HTTP_HOST}" pattern="^mysite\.com$" />
    </conditions>
    <action type="Redirect" url="http://www.mysite.com/{R:1}" />
   </rule>
  </rules>
 </rewrite>
</system.webServer>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM