web.config слияние 2 правил в 1

#web-config #rules

#web-config #Правила

Вопрос:

У меня есть 3 правила, которые действовали на основе 1. Скрытие расширения HTML 2. Перенаправление html 3. не-www на www.

Однако это вызвало 2 перенаправления 301, когда пользователь переходит к http://example.net/content -> http://www.example.net/content.html -> http://www.example.net/content

 <rule name="Hide .html ext">
      <match ignoreCase="true" url="^(.*)"/>
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
        <add input="{REQUEST_FILENAME}.html" matchType="IsFile"/>
      </conditions>
      <action type="Rewrite" url="{R:0}.html"/>
</rule>
<rule name="Redirecting .html ext" stopProcessing="false">
      <match url="^(.*).html"/>
      <conditions logicalGrouping="MatchAny">
        <add input="{URL}" pattern="(.*).html"/>
      </conditions>
      <action type="Redirect" url="{R:1}"/>
</rule>
<rule name="Redirect to www" stopProcessing="true">
      <match url="(.*)" />
      <conditions trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="^example.net$" />
      </conditions>
      <action type="Redirect"
        url="{MapProtocol:{HTTPS}}://www.example.net/{R:1}" />
</rule>
  

У меня есть специальное требование, позволяющее выполнять только 1 перенаправление 301 для этой страницы. Я пробовал несколько способов, но безрезультатно. Каков наилучший способ, которым я могу это попробовать?

Спасибо!

Ответ №1:

Добавить в качестве второго правила :

 <rule name="Redirect to www   .html ext" stopProcessing="true">
      <match url="(.*)" />
      <conditions trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="^example.net$" />
        <add input="{URL}" pattern="(.*).html"/>
      </conditions>
      <action type="Redirect"
        url="{MapProtocol:{HTTPS}}://www.example.net/{R:1}" />
</rule>