简体   繁体   中英

Rewrite on IIS7 not working correctly with “www”

I have problem with my redirection rule. I want my pages will have the "www" prefix. It works on some pages, while on other it simply not doing anything. This is my rule:

    <rule name="WWW Rewrite" enabled="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>

Any help will be appreciated!

You could try this ^(www.)?([.a-zA-Z0-9]+)$

that would match if the page doesnt currently have a "www." and should match if it does or doesnt.

you would need to edit the action to this to pick up the second group <action type="Redirect" url="http://www.{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />

Here is your rule, you just gotta check if your host doesn't match expected host and then redirect to the correct url.

  <rules>
    <rule name="Canonical Host Name" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTP_HOST}" negate="true" pattern="^www\.domain\.com$" />
      </conditions>
      <action type="Redirect" url="http://www.domain.com/{R:1}" redirectType="Permanent" />
    </rule>
  </rules>

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