Need Rewrite Rule That Allows Optional or NULL Parameters [Answered]RSS

3 replies

Last post Oct 17, 2019 01:24 AM by cyrussullivan

  • Need Rewrite Rule That Allows Optional or NULL Parameters

    Oct 13, 2019 10:36 PM|cyrussullivan|LINK

    Hi,

    I am working on an ASP.Net 4.7 website in which I want my wildcard subdomain to support multiple optional parameters. The end result should rewrite something like https://city-state.example.com to https://example.com/local/state/city where the latter URL uses RouteData.Values. My current rewrite rule works fine of applied to just https://city-state.example.com but if I leave city blank then routing fails. 

    My web.config has this:
    
    <rule name="Rewrite subdomains">
            <match url="^/?$" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^(.+)-(.+)\.domain\.com$" />
            </conditions>
            <action type="Rewrite" url="/local/{C:2}/{C:1}" />
        </rule>
    
    My RouteConfig.cs has this:
    
    routes.MapPageRoute("local/state", "local/{state}", "~/local/Default.aspx");
                routes.MapPageRoute("local/state/city", "local/{state}/{city}", "~/local/Default.aspx");
    
    
    

    I was hoping that this rule would work because if C:1 is empty then the URL would be rewritten as local/state/ but for whatever reason something is being added there. Whenever I try a subdomain that is just https://state.example.com it redirects to the root which happens whenever routing fails under certain conditions like having an invalid parameter. I don't know what value is being passed there. I would think that if the application received something like local/state/null that it would ignore null and not treat null as a city, so I think there might be some character being passed like a space or something.

  • Re: Need Rewrite Rule That Allows Optional or NULL Parameters

    Oct 14, 2019 12:28 AM|cyrussullivan|LINK

    This seems to work:

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

    I still need to keep the site navigation from changing though. All my relative links on the master page are changing from the root to subdomain/root even though they are all coded using /url or ~/url

  • Re: Need Rewrite Rule That Allows Optional or NULL Parameters

    Oct 14, 2019 01:41 AM|Jalpa Panchal|LINK

    Hi,

    You could use failed request tracing to check why this is happening. you could also use an outbound rule to restrict page links from rewriting.

    Could you please share the sample code to reproduce the issue if possible? 

    Regards,

    Jalpa

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue.
    If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
  • Re: Need Rewrite Rule That Allows Optional or NULL Parameters

    Oct 17, 2019 01:24 AM|cyrussullivan|LINK

    Adding outbound rules worked.