How Do I Pass Route Parameters via URL Rewriting if They Appear at the End of the Sting? [Answered]RSS

3 replies

Last post Oct 20, 2019 09:45 PM by cyrussullivan

  • How Do I Pass Route Parameters via URL Rewriting if They Appear at the End of the Sting?

    Oct 18, 2019 06:12 AM|cyrussullivan|LINK

    Hi,

    I have a rewrite rule that passes parameters in the subdomain name to my application just fine but oddly never passes parameters that appear after the domain name in the URL.

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

    I know the problem is not with the routing configuration because I have tested the routes without being rewritten and they work fine. This rule works great for something like http://city-state.example.com by passing city and state routevalues to ~/folder/state-city  but it never seems to pass anything from condition 4. As a result I get a 404 for stuff like http://city-state.example.com/category. My understanding of regex is that .* is a catch all that matches everything, so by having the condition set to (.*) that should catch everything after the m in .com but this does not seem to be passing anything at all. It should catch /category and add it to the rewritten URL.

  • Rovastar Rovastar

    5459 Posts

    MVP

    Moderator

    Re: How Do I Pass Route Parameters via URL Rewriting if They Appear at the End of the Sting?

    Oct 18, 2019 01:56 PM|Rovastar|LINK

    condition 4 will always be empty is that just looks at the HTTP_HOST part of the request.

    A simply way for the urIstem would be

    <match url="(.*)" />

    and have

    /{R:1} instaed of {C:4}


    Troubleshoot IIS in style
    https://www.leansentry.com/
  • Re: How Do I Pass Route Parameters via URL Rewriting if They Appear at the End of the Sting?

    Oct 19, 2019 05:45 AM|cyrussullivan|LINK

    Replacing {C:4} with /{R:1} causes a 500 error for the whole site.

    The log says 

    <div class="content-container"> <div class="summary-container hidden" id="section_errors">
    <div id="section_errors_details">
    <div id="section_errors_73_details">
    The expression "{ToLower:/local/{C:3}{C:2}{C:1}/{R:1}}" cannot be expanded.
    </div>
    </div>
    </div> </div>
  • Re: How Do I Pass Route Parameters via URL Rewriting if They Appear at the End of the Sting?

    Oct 20, 2019 09:45 PM|cyrussullivan|LINK

    I fixed it. Instead of {c:4} I needed to use {PATH_INFO} so the final rule looks something like:

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

    This will effectively feather rout values between subdomains and subfolders. So in this case locations go in the subdomain and categories go in the subfolders. In this case users can create subdomains for their city if it has not already been added to the site, so there is no limit to how many US cities can be added. I like using hyphen separated parameters in the subdomain because according to what I have read my wildcard SSL will cover stuff like *.example.com but not *.*.example.com.

    Now I just need to get my remote server upgraded to Windows Server 2016 with IIS 10 so that I can use the wildcard subdomain binding feature. 

    I also use the following outbound rules to properly rewrite URLs in the HTML on the page. 

    <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://example.com/{R:1}" />
            </rule>
            <preConditions>
              <preCondition name="ResponseIsHtml1">
                <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
              </preCondition>
            </preConditions>
          </outboundRules>

    Conclusion

    The above is a useful example to follow for anyone that wants to use URL rewriting for combinations of subdomains and subfolders.