Search code examples
c#asp.nethttp-redirectiisweb-config

How to redirect via web.config ASP.NET


I need to redirect from example.com/landing to example.com/blog/info/landing. I wrote a rule in web.config

<rule name="Atlanta redirect" stopProcessing="true">
  <match url="landing" />
  <action type="Redirect" url="https://www.example.com/blog/info/landing" redirectType="Permanent" />
</rule>

but if url matches "landing" word then it redirect to example.com/blog/info/landing too.

For example correct redirect: example.com/landing -> example.com/blog/info/landing

Wrong redirect example.com/somepage/1/landing -> example.com/blog/info/landing


Solution

  • Use a regex that matches /landing exactly:

    <match url="^landing$" />
    

    (IIS removes the leading / when doing regex matches)

    The ^ and $ symbols are Regex Anchors that match the start and end of the string respectively.