简体   繁体   中英

URL rewrite rule (iis): redirect ONLY www.example.com and www.example.com/default.aspx

I am trying to do a rewrite to redirect the following pages ONLY:

www.example.com/

www.example.com/default.aspx

I want to let all other requests pass through. I know I'm pretty close but I just can't seem to get it perfect. Thanks in advance for any & all help!

Here's what I have so far:

<rewrite>
   <rules>

    <rule name="Base Redirect" stopProcessing="true">
      <match url="" />
         <conditions>
          <add input="{HTTP_HOST}" pattern="www.example.com" ignoreCase="true" negate="false" />
      </conditions>
      <action type="Redirect" redirectType="Permanent"  url="shop.example.com" appendQueryString="true" />
    </rule>

    <rule name="Default.aspx Redirect" stopProcessing="true">
      <match url="default.aspx" />
         <conditions>
          <add input="{HTTP_HOST}" pattern="www.example.com" ignoreCase="true" negate="false" />
      </conditions>
      <action type="Redirect" redirectType="Permanent"  url="shop.example.com" appendQueryString="true" />
    </rule>

  </rules>
</rewrite>

Problem is it's sending anything containing default.aspx (/pages/default.aspx, etc) and we don't want to redirect anything but the root level default.aspx.

Felix

Try this (basically adding the "^" character to tell regex to only match when it is at the beginning:

<rule name="Base Redirect" patternSyntax="ECMAScript" stopProcessing="true">
  <match url="^$" />
  <conditions>
                <add input="{HTTP_HOST}" pattern="www.example.com" />
  </conditions>
  <action type="Redirect" url="http://shop.example.com/" appendQueryString="true" redirectType="Permanent" />
</rule>

<rule name="Default.aspx Redirect" stopProcessing="true">
  <match url="^default.aspx" />
  <conditions>
                <add input="{HTTP_HOST}" pattern="www.example.com" />
  </conditions>
  <action type="Redirect" url="http://shop.example.com" appendQueryString="true" redirectType="Permanent" />
</rule>

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