Sitecore - rewrite " " to "-" in urls but still allow dashes as legal item names
Asked Answered
B

2

5

I've read a half dozen guides on rewriting spaces to something more friendly in Sitecore, but all of them rely on Sitecore's <encodeNameReplacements/> element which also reverses the replacement requiring "-" to be an illegal character for names.

The problem with this is that the url of our application has a "-" in the hostname. Sitecore rewrites this resulting in a 404.

Does anyone have ideas on how to do this url rewriting in Sitecore without relying on <encodeNameReplacements and still allowing "-" as a legal item name character? Our current best idea is to use something slighty more complex than a plain "-" such as "--" or "_". This isn't a very good idea, so I'd appreciate any insight you guys have on the matter.

EDIT: We are running a multi-site setup with Sitecore 6.5

Barm answered 25/4, 2013 at 21:12 Comment(2)
Are you running a multisite implementation?Disparagement
Yes. We are running two sites.Barm
J
2

So, if I am assuming correctly, you want to replace spaces in item names with some other SEO-friendlier character. Whatever replacement you configure, you would need to apply the transformation on both sides of the equation (pun intended). So '--' or '_' will have to become illegal item name characters.

I generally think it's a bad idea to do this and would rather have my content editors determine the exact urls to their content.

But if you absolutely need to implement this as a rule, one of the solutions out there is to implement a custom handler to change your real item name while leaving the item display name in its original form.

Hope this helps.

Jube answered 25/4, 2013 at 23:28 Comment(2)
He's talking about the hostname, not the item names. But, although I generally agree with that article, it's not always possible in a multiple lingual site when people want to use localized URLs using the display name field.Disparagement
The custom handler is what we were looking for. Thank you!Barm
D
2

Which version and build of Sitecore are you using?

I've just taken a look through Sitecore.Links.LinkProvider in Sitecore.Kernel and the BuildItemUrl method only replaces characters encodeNameReplacements on the path part of the URL so it should leave your hostname alone.

I would expect the same to happen to reverse, and looking at Sitecore.Pipelines.HttpRequest.SiteResolver the SiteContext is resolved by matching the requested Uri to the defined sites. Further down the process Sitecore.Pipelines.HttpRequest.ItemResolver decodes the Item Url but the Site has already been resolved at this stage.

I presume you have set the hostName attribute on the <site> elements? Having you tried setting the targetHostName attribute as well? Have you tried setting multiple hostNames, I don't expect it to work with spaces but worth a try if what you are saying is true:

<site name="website" hostName="my-site.com|my site.com"  ... />

I tried replicating your issue on my local machine but it worked as expected for me... Unless you are working with very strange character sets then this is still the best way of encoding names in my opinion.

Disparagement answered 25/4, 2013 at 23:29 Comment(4)
I am replacing two things <replace mode="on" find=" " replaceWith="-" />(Space with hyphen) <replace mode="on" find="" replaceWith="-" />(underscore with hyphen) But in the case of underscore() its changing in hyphens(-) but redirecting to 404, any idea?Patentor
@GauravAgarwal 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. You are better using an event handler to deal with these in the first place. I'll have a blog post on Item naming in the next few days you will find helpful.Disparagement
yeah Event handler is good idea but I want to do so this way if possible, its working fine in the case of '$', <replace mode="on" find="$" replaceWith="-" /> after <replace mode="on" find=" " replaceWith="-" />, both are working but only underscore is creating problem.Patentor
Also asked a question #31005313Patentor

© 2022 - 2024 — McMap. All rights reserved.