Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sitecore - rewrite “_” to “-” in urls but going to 404

Tags:

sitecore

I am using Sitecore 7.5 and replacing two things

<replace mode="on" find=" " replaceWith="-" />(Space with hyphen) 
<replace mode="on" find="_" replaceWith="-" />(underscore with hyphen) 

Replacing space(" ") with hyphne(-) is working fine but in the case of underscore(_) its changing in hyphens(-) but redirecting to 404, any idea?

We can manage this by Event handler but don't want to do that way.

like image 551
Gaurav Agarwal Avatar asked Jun 23 '15 13:06

Gaurav Agarwal


1 Answers

I commented on another answer that the problem is that when resolving the items, the incoming has the reverse replacements applied. Since you have 2 replacements both mapping to "-" then on incoming it fails since it tries to replace "-" with space initially, but some of those hyphens should be underscore but it has no idea which one should be which.

For example, given path: /path to some/item_url then the generated url is /path-to-some/item-url.

On incoming, the reverse replacements are replied, and Sitecore is now looking for /path to some/item url, which does not exists and so a 404 is thrown. Since the "_" (underscore) was replaced by a "-" (hyphen), on incoming the "-" (hyphen) is replaced with " " (space). Since there are no hyphens left to replace, it cannot replace with underscore.

Take a look in Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel and you will see a call to MainUtil.DecodeName(args.Url.ItemPath) where the EncodeNameReplacements are applied.

You are better using an event handler to deal with these in the first place so you don't then need worry about any kind of mapping.

like image 159
jammykam Avatar answered Sep 22 '22 04:09

jammykam