web.config configuration for rewriting subdirectories to querystrings RRS feed

  • Question

  • User-1852603773 posted


    Let's say my website displays results for different restaurants in cities.

    Currently my website is:


    city and pr(ovince) are passed to search.asp to display results.

    This works fine but for SEO purposes I'd like to change it.

    Ideally I'm looking to do this:


    to rewrite to the above querystring.

    The issue is that people are able to add new cities at any time, which means that the rewrite shouldn't rely upon a pre-determined list of acceptable provinces or cities. Also I'm concerned that if I do use a rewrite, what happens if I happen to call a legitimate URL such as:


    I wouldn't want that URL to be rewritten.

    I'm feeling my best (or only) option would be something like:


    and have the rewrite look for "places"

    So far I've come up with: ^(.*/)www.example.com/search.asp\?city=([^=&]+)&(?:amp;)?pr=([^=&]+)$

    This however doesn't work. I have difficulty with the expressions that rewriting uses and could use some assistance please.

    Thank you in advance for any help.

    Saturday, March 7, 2020 1:03 AM

All replies

  • User753101303 posted


    See https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module

    It would be rather the other way round ie something such as :

        <rule name="Rewrite to article.aspx">
          <match url="^places/([a-zA-Z]+)/([a-zA-Z]+)$" />
          <action type="Rewrite" url="search.asp?city={R:1}&amp;pr={R:2}" />

    and you should update your app to generate the correct urls.

    Edit;: the purpose of url rewriting is not to "fix" the url generated by your app but rather to tell IIS which actual url should be processed when a particular pattern matches the incoming request pattern.

    Sunday, March 8, 2020 12:46 PM