locked
Adding Additional Rewrite Rule Conditions Always Causes a 500 Error RRS feed

  • Question

  • User-939035612 posted

    Hi,

    What is it about this rewrite rule that causes a 500 error is any additional conditions are added?

    <rule name="Rewrite subdomains">
            <match url=".*" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^(^$|[^-]*)(^$|[\-]*)([^-]*)\.domain\.com$" />
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            </conditions>
            <action type="Rewrite" url="{ToLower:/local/{C:3}/{C:1}}" />
        </rule>

    The IsFile condition only works if both c:3 and c:1 are present but if c:3 is present and c:1 is empty then IsFile is completely ineffective and not scripts load. I hoped adding another condition might help but if anything is added it causes a 500 error..

    Monday, October 14, 2019 3:02 AM

Answers

  • User-939035612 posted

    Actually I've found a solution that works for what I need. That rewrite rule worked but I just needed to serve scripts from the root and not the subdomain. I added an outbound rule and it works good.

     <rule name="Rewrite subdomains">
            <match url=".*" />
            <conditions>
              <add input="{HTTP_HOST}" pattern="^(^$|[^\-]*)(^$|[\-]?)([^\-]\w)\.domain\.net$" />
              <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            </conditions>
            <action type="Rewrite" url="{ToLower:/local/{C:3}/{C:1}}" />
        </rule>
          
          </rules>
          <outboundRules>
            <rule name="RewriteRelativePaths" preCondition="ResponseIsHtml1">
              <match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^/(.*)" negate="false" />
              <action type="Rewrite" value="http://domain.net/{R:1}" />
            </rule>
            <preConditions>
              <preCondition name="ResponseIsHtml1">
                <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
              </preCondition>
            </preConditions>
          </outboundRules>

    Thanks for the feedback though.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 16, 2019 7:51 AM

All replies

  • User288213138 posted

    Hi CopBlaster,

    The IsFile condition only works if both c:3 and c:1 are present

    According to your description, I can't reproduce your question.

    but if c:3 is present and c:1 is empty then IsFile is completely ineffective and not scripts load

    Can you post the url of you tested?

    but if anything is added it causes a 500 error

    What Rewrite Rule Conditions have you added to cause 500error?

    If you want to troubleshoot URL rewriting rules question, you can try to use FRT tool.

    Best regards,

    Sam 

    Tuesday, October 15, 2019 6:36 AM
  • User-939035612 posted

    I have changed the rule a slightly and I almost don't see the need to change it. It works just fine on every browser I've tested except for Edge. On edge something like http://subdomain.example.com loads fine but example.com cannot be found. I can't give you a link because it is on a local development server.

    <rule name="Rewrite subdomains">
            <match url=".*" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^(^$|[^-]*)(^$|[-]?)([^-]*)\.example\.com$" />
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            </conditions>
            <action type="Rewrite" url="{ToLower:/local/{C:3}/{C:1}}" />
        </rule>

    Are you familiar with anything about Edge that might cause rewrite rules not to work?

    Tuesday, October 15, 2019 8:03 PM
  • User-939035612 posted

    Here is an addition that causes a 500 error

    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />

    Wednesday, October 16, 2019 3:18 AM
  • User288213138 posted

    Hi CopBlaster,

    On edge something like http://subdomain.example.com loads fine but example.com cannot be found. I can't give you a link because it is on a local development server.

    I need your tested url to reproduce your question. such as: http://subdomain.example.com

    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />

    I tested this addition, it works fine in my side:

    And can you show me the 500 error message?

    Best regards,

    Sam

    Wednesday, October 16, 2019 7:32 AM
  • User-939035612 posted

    Actually I've found a solution that works for what I need. That rewrite rule worked but I just needed to serve scripts from the root and not the subdomain. I added an outbound rule and it works good.

     <rule name="Rewrite subdomains">
            <match url=".*" />
            <conditions>
              <add input="{HTTP_HOST}" pattern="^(^$|[^\-]*)(^$|[\-]?)([^\-]\w)\.domain\.net$" />
              <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            </conditions>
            <action type="Rewrite" url="{ToLower:/local/{C:3}/{C:1}}" />
        </rule>
          
          </rules>
          <outboundRules>
            <rule name="RewriteRelativePaths" preCondition="ResponseIsHtml1">
              <match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^/(.*)" negate="false" />
              <action type="Rewrite" value="http://domain.net/{R:1}" />
            </rule>
            <preConditions>
              <preCondition name="ResponseIsHtml1">
                <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
              </preCondition>
            </preConditions>
          </outboundRules>

    Thanks for the feedback though.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 16, 2019 7:51 AM