0
votes

Web.config URL rewrite is giving me headaches :-) I'm trying to setup rewrite rules following these conditions:

  1. It should produce only one single redirect
  2. Ignore resources
  3. remove default.aspx
  4. remove trailing slash
  5. lowercase (not querystring) when not using custom file extentions
  6. force www
  7. force https

All this is working great, except when the original url allready has WWW in it. If the url has WWW then is will follow all rules but won't redirect to HTTPS.

Any help would be greatly appreceated!

Here's what I have so far (inspired by this great article):

  <rules>


    <rule name="WhiteList - resources" stopProcessing="true">
      <match url="^resources/" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
      <action type="None" />
    </rule>

    <rule name="SEO - remove default.aspx" stopProcessing="false">
      <match url="(.*?)/?default\.aspx$" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_METHOD}" pattern="GET" />
      </conditions>
      <action type="Rewrite" url="_{R:1}" />
    </rule>

    <rule name="SEO - Remove trailing slash" stopProcessing="false">
      <match url="(.+)/$" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_METHOD}" pattern="GET" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
      </conditions>
      <action type="Rewrite" url="_{R:1}" />
    </rule>

    <rule name="SEO - ToLower" stopProcessing="false">
      <match url="(.*)" ignoreCase="false" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{REQUEST_URI}" pattern="\.pngx|\.jpgx|\.gifx|\.bmpx|\.orcx" negate="true" />
        <add input="{HTTP_METHOD}" pattern="GET" />
        <add input="{R:1}" pattern="[A-Z]" ignoreCase="false" />
      </conditions>
      <action type="Rewrite" url="_{ToLower:{R:1}}" />
    </rule>

    <rule name="ADD WWW IF NOT PRESENT" stopProcessing="true">
      <match url="^(_*)(.*)" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="^www\.testssl\.orca-bree\.be$" negate="true" />
        <add input="{HTTP_METHOD}" pattern="GET" />
      </conditions>
      <action type="Redirect" url="https://www.testssl.orca-bree.be/{R:2}" redirectType="Permanent" />
    </rule>

    <rule name="redirect" stopProcessing="true">
      <match url="^(_+)(.*)" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_METHOD}" pattern="GET" />
      </conditions>
      <action type="Redirect" url="{R:2}" redirectType="Permanent" />
    </rule>

  </rules>
1

1 Answers

0
votes

I didn't test it, but it should work:

<rule name="Redirect to www" patternSyntax="ECMAScript" stopProcessing="true">
  <match url=".*"></match>
  <conditions logicalGrouping="MatchAny">
    <add input="{HTTP_HOST}" pattern="^mydomain.be$"></add>
    <add input="{HTTPS}" pattern="off"></add>
  </conditions>
  <action type="Redirect" url="https://www.mydomain.be/{R:0}" redirectType="Permanent" appendQueryString="true"></action>
</rule>
<rule name="Remove trailing slash" stopProcessing="true">
  <match url="(.*)/$" />
  <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
  </conditions>
  <action type="Redirect" redirectType="Permanent" url="https://www.mydomain.be/{R:1}" />
</rule>

What I've changed:

  • Actions of your rules. Now they contain full domain name with www and https
  • Added MatchAny to your conditions in the first rule