Quantcast
Channel: URL Rewrite Module
Viewing all articles
Browse latest Browse all 2482

Need Rewrite Rule That Allows Optional or NULL Parameters

$
0
0

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.


Viewing all articles
Browse latest Browse all 2482

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>