1

I'm trying to develop a custom route for this use case.
The following URL patterns:

https://example.com/new/Brand-1/product-1.htm

https://example.com/used/Brand-2/product-2.htm

should be redirected to https://example.com/products

The Brand & Product names in the URL may change, but the relative root will always be either /new or /used.

When I browse this URL - https://example.com/used/mybrand/myproduct.htm, it throws the error: enter image description here

Here is the code:

RouteConfig.cs:

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");            

            routes.MapRoute("OldUsed", "used/{make}/{name}", new
            {
                controller = "RedirectToListing",
                action = "RedirectOldVdpToVlp",
                make = "",
                name=""
            });

        }
    }

InitializeRoutes.cs:

public class InitializeRoutes : Sitecore.Mvc.Pipelines.Loader.InitializeRoutes
    {
        public override void Process(PipelineArgs args)
        {
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }        
    }

z_Feature.Base.Pipelines.config:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <initialize>
        <processor type="Feature.Base.Pipelines.InitializeRoutes, Feature.Base" patch:before="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc']" />
      </initialize>
    </pipelines>
  </sitecore>
</configuration>

Is this the right way.

5
  • 3
    If it helps you could try implementing rewrite/redirect rules in IIS instead of having custom routes defined Commented Mar 24, 2021 at 14:12
  • @CristiVulturar I also need to handle some business logic, like getting the brand name from the old url and then redirect to the listing page with that as a query string param. Would that be possible. If yes, could you please share any references.
    – sukesh
    Commented Mar 24, 2021 at 14:14
  • 2
    You can use redirect rules with regular expressions and backreferences. That is possible. This is just one blog about it: isitoktocode.com/post/…
    – Gatogordo
    Commented Mar 24, 2021 at 14:36
  • 1
    Did you add htm extension to Allowed extensions in <processor type="Sitecore.Pipelines.PreprocessRequest.FilterUrlExtensions, Sitecore.Kernel">? If this is not done Sitecore will not run httpRequestBegin pipeline
    – Jeroen
    Commented Mar 24, 2021 at 15:19
  • @Jeroen yes i did
    – sukesh
    Commented Mar 24, 2021 at 15:36

1 Answer 1

1

You can use IIS Rewrite inbound rules to accomplish this, for example:

<rule name="Property URL Rewrite" stopProcessing="true">
   <match url="^new/Brand-([0-9]+)/Product-([0-9]+)$" />
   <action type="Rewrite" url="/products?brand={R:1}&amp;product={R:2}" appendQueryString="true" logRewrittenUrl="false" />
</rule>
                    
<rule name="Property URL Rewrite" stopProcessing="true">
    <match url="^used/Brand-([0-9]+)/Product-([0-9]+)$" />
    <action type="Rewrite" url="/products?brand={R:1}&amp;product={R:2}" appendQueryString="true" logRewrittenUrl="false" />
</rule>

If you have links on the pages aswell you might need to create an outbound rule aswell to handle them, for example https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/creating-outbound-rules-for-url-rewrite-module

2
  • Thank you. But can this not be done from code. As there could be some complex logic to be handled and also Brand & product were just examples. The names could be any alphanumeric strings.
    – sukesh
    Commented Mar 24, 2021 at 15:02
  • For alphanumeric strings you can just change [0-9] to [0-9a-z]. From code you could also look into how the sitecore 301 redirect module is implemented, although that is doing redirect and not rewrite. We have relied on rewrite rules as it is easier to setup the parameters instead of custom routes. Commented Mar 24, 2021 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.