Search code examples
asp.net-coreiisurl-rewritingweb-configasp.net-mvc-routing

IIS Redirect Permanent to part of old URL


I am trying to redirect permanent this using IIS rewrite

www.domain.com/category/sub-category/product-slug OR www.domain.com/category/product-slug

to

www.domain.com/product-slug

Old URLs are from magento2 while new site is on nopCommerce

Basically I want every URL to be redirected to domain.com/product-slug. How I can achieve this?

Any help will be highly appreciated.


Solution

  • You can try below rules:

    <rule name="test1" stopProcessing="true">
     <match url="(.*)" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="www.test.com" />
        <add input="{REQUEST_URI}" pattern="^/([^/]+)/([^/]+)/([^/]+)$" />
      </conditions>
      <action type="Redirect" url="http://www.test.com/{C:1}/{C:3}" />
    </rule>
    

    and

    <rule name="test2" stopProcessing="true">  
        <match url="(.*)" />
            <conditions>
              <add input="{HTTP_HOST}" pattern="www.test.com" />
              <add input="{REQUEST_URI}" pattern="^/([^/]+)/([^/]+)$" />
             </conditions>
        <action type="Redirect" url="http://www.test.com/{C:1}/{C:2}" />
     </rule>
    

    The body cannot contain "http://www. domain.com", so I use www.test.com instead.