1

I'm getting errors on my rewrite rules when the URL contains a special character:

This URL http://www.example.com/bungalow/rent/state/texas/street/exloër/exloër with this rewrite rule:

    <rule name="rentals by proptype+state+city+street">
      <match url="^([a-zA-Z0-9-+]+)/rent/state/([a-zA-Z-+]+)/street/([a-zA-Zë-+]+)/([0-9a-zA-Zë-+']+)$" />
      <action type="Rewrite" url="search_new.aspx?proptype={R:1}&amp;state={R:2}&amp;city={R:3}&amp;street={R:4}" />
    </rule>

results in a 500 error

This URL http://www.example.com/bungalow/rent/state/texas/street/exloër/exloër with this rewrite rule:

    <rule name="rentals by proptype+state+city+street">
      <match url="^([a-zA-Z0-9-+]+)/rent/state/([a-zA-Z-+]+)/street/([a-zA-Z-+]+)/([0-9a-zA-Z-+']+)$" />
      <action type="Rewrite" url="search_new.aspx?proptype={R:1}&amp;state={R:2}&amp;city={R:3}&amp;street={R:4}" />
    </rule>

results in a 404 error

How can I handle special characters in the rewrite rule?

update 1

The URL in question is displayed with a ë character, but when I copy the address, it's escaped to this %c3%abr With this rule I still get a 404 error:

    <rule name="rentals by proptype+state+city+street">
      <match url="^([a-zA-Z0-9-+]+)/rent/state/([a-zA-Z-+]+)/street/([a-zA-Z%-+]+)/([0-9a-zA-Z%-+']+)$" />
      <action type="Rewrite" url="search_new.aspx?proptype={R:1}&amp;state={R:2}&amp;city={R:3}&amp;street={R:4}" />
    </rule>

So I guess the real question would be, how to handle % characters in the rewrite rule?

Adam
  • 6,041
  • 36
  • 120
  • 208
  • You don't. Those are not allowed in an URL. You have to escape them before you make them into an url. – VDWWD Sep 07 '17 at 19:29
  • Maybe this will help: https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference – VDWWD Sep 08 '17 at 07:13
  • @VDWWD thanks. I'm indeed using that module. But after reading that page I still can't explain why my link does not work and throws an error. Could you explain it? – Adam Sep 10 '17 at 17:33

1 Answers1

3

Your last attempt had almost correct regex, you just had one small mistake (forgot to add 0-9 to third block). Correct regexp is:

^([a-zA-Z0-9-+]+)/rent/state/([a-zA-Z-+]+)/street/([a-zA-Z0-9%-+]+)/([0-9a-zA-Z%-+']+)$

But in rewrite rule you need to use variable {UNENCODED_URL}.

Working example is:

<rule name="rentals by proptype+state+city+street" stopProcessing="true">
    <match url=".*" />
    <conditions>
        <add input="{UNENCODED_URL}" pattern="^/([a-zA-Z0-9-+]+)/rent/state/([a-zA-Z-+]+)/street/([a-zA-Z0-9%-+]+)/([0-9a-zA-Z%-+']+)$" />
    </conditions>
    <action type="Rewrite" url="search_new.aspx?proptype={C:1}&amp;state={C:2}&amp;city={C:3}&amp;street={C:4}" />
</rule>

UPD

After example from comments:

Your url: http://www.example.com/bungalow/rent/state/north-dakota/stre‌​‌​et/savanah/%27s-gr‌​ac‌​eland has some hidden special characters (even SO can't parse it properly). You can check how it's encoded here: https://www.urlencoder.org/.

Becased on that i changed the regexp in the rule with following:

<rule name="rentals by proptype+state+city+street" stopProcessing="true">
    <match url=".*" />
    <conditions>
        <add input="{UNENCODED_URL}" pattern="^/([a-zA-Z0-9\-+]+)/rent/state/([a-zA-Z\-+]+)/([a-zA-Z0-9%\-+]+)/([a-zA-Z0-9%\-+]+)/([0-9a-zA-Z%\-+']+)$" />
    </conditions>
    <action type="Rewrite" url="search_new.aspx?proptype={C:1}&amp;state={C:2}&amp;city={C:4}&amp;street={C:5}" />
</rule>
Victor Leontyev
  • 8,488
  • 2
  • 16
  • 36
  • Alright, it worked on the sample URL I gave, but why is your rewrite rule still throwing a 404 on this URL: `http://www.example.com/bungalow/rent/state/north-dakota/stre‌​et/savanah/%27s-grac‌​eland` and this one (not sure if querystring parameter impacts your rule) `http://www.example.com/bungalow/rent/state/north-dakota/stre‌​et/savanah/%27s-grac‌​eland?pricefrom=2250` – Adam Sep 12 '17 at 21:44
  • 1
    For anyone using this rule, be aware `{UNENCODED_URL}` may contain query strings too. So this breaks the rule when the URL contains any query string, even if it's just a query separator (`?`). See here https://stackoverflow.com/questions/46229792/regex-for-url-rewrite-with-optional-query-string-parameters/46259743#46259743 – Adam Sep 17 '17 at 01:02