.NET Url rewrite the root url

2019-05-14 11:06发布

问题:

I have the follow rule in IIS7:

    <rule name="RewriteAll" stopProcessing="true">
      <match url="^([^/]+)/?$" />
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{URL}" negate="true" pattern="\.axd$" />
        <add input="{URL}" negate="true" pattern="\.png$" />
        <add input="{URL}" negate="true" pattern="\.gif$" />
        <add input="{URL}" negate="true" pattern="\.jpg$" />
        <add input="{URL}" negate="true" pattern="\.css$" />
        <add input="{URL}" negate="true" pattern="\.js$" />
      </conditions>
      <action type="Rewrite" url="default.aspx?page={R:1}" />
    </rule>

That catch urls like http://mysite.com/contact/

I can't figure out with rule to use to catch http://mysite.com/ and send to for example home.aspx. Can anyone help me with the match regex

回答1:

I fixed it by adding the following rule:

 <rule name="Index Request" enabled="true" stopProcessing="true">
     <match url="^$" />
     <action type="Rewrite" url="index.aspx" logRewrittenUrl="true" />
 </rule>


回答2:

Why not just setup a canonical name (CName) on your DNS server to automatically redirect http://domain.com to http://www.domain.com?

See the CName explanation on wikipedia