My old site used underscores to separate words in page name, i.e, "/centers_and_services/somepage.aspx".
Of course, Umbraco uses dashes. So what would the URL Rewrite rule look like that would replace all underscores with dashes so my pages don't break when linked from Google, etc.? So I need this:
Even easier... change the way umbraco creates the nice urls! No rewriting necessary. Just tell umbraco to use an underscore instead of a dash for 'spaces' in nodenames.
Open the /config/umbracoSettings.config file. Near the bottom you'll find a bunch of entries for character replacement. One of the first ones replaces a space with a dash. Change it to use an underscore instead. Save the file.
Now force umbraco to recalculate all the site's urls by right-clicking each "level 1" node in the content tree. Select the 'Publish' menu and tick the Include Children box. Repeat for all the top-most nodes in your content tree. (note: using the 'republish entire site' will not recalculate the urls... you need the 'publish' menu for this).
Or, if you want to keep most of the urls using dashes and only a few need to use underscores, create a docType property with an alias of 'umbracoUrlName'. Whatever you put in that field will over-ride the default NiceUrl that umbraco would normally use for that page.
URL rewriting uses a regex pattern so you could replace all underscores in incoming requests to dashes for the pages in umbraco. The url rewriter documentation is excellent. You make all edits in the /config/urlrewriting.config file (which also references the docs and gives a link).
Shout if you have trouble coming up with the proper rule.
There I go shooting my mouth off thinking it would be easy... but when I tried it... I couldn't figure it out either!
Perhaps someone else can do a global replace of all "_" characters in the incoming url with "-" characters in the destination url but I can't crack this nut; at least not with the URLrewritingNet.UrlRewriter.
There are, of course, other rewriting tools you can use on your site and some of those would be able to the job I think.
Or, maybe reconsider some of the other options we'd discussed above?
Note: I think the goal is to go from urls that have underscores from the old site and change them to use dashes in umbraco. Therefore I think you want to change the rewrite rules to go the other way... from underscore to dashes.
Question: You could end up with a lot of these entries to handle an arbitrary number of underscores/dashes. Is there any way to do a greedy replace? (I suspect not but it would be great it were possible!)
Thanks again, Matt. I know who I'll be contacting when I need any rewrite or regex rules in the future ;)
Ooops, thats what I get for rushing. You are correct, the hyphens and underscores should be as your example.
I can't say I have come across anything that would do it in one statement, but I like to keep my regex' as simple and readable as I can, so I would personaly prefer several readable rules, rather than one over complex rule. But I guess that also depends on how long you expect your URLs to be.
You could also have gone about this the other way, and have umbraco replace all spaces by underscores instead of dashes, this behavior is defined in a config file:
Hey guys, is someone know how to manage the same issue on newer Umbraco version? is there a better solution now instead of using regex redirect? thanks.
URL Rewriting: Replace Dash with Underscore
My old site used underscores to separate words in page name, i.e, "/centers_and_services/somepage.aspx".
Of course, Umbraco uses dashes. So what would the URL Rewrite rule look like that would replace all underscores with dashes so my pages don't break when linked from Google, etc.? So I need this:
/centers_and_services/somepage.aspx > /centers-and-services/somepage.aspx
I realize I can use umbracoUrlAlias, but I'd like to automate it to some degree.
Even easier... change the way umbraco creates the nice urls! No rewriting necessary. Just tell umbraco to use an underscore instead of a dash for 'spaces' in nodenames.
Open the /config/umbracoSettings.config file. Near the bottom you'll find a bunch of entries for character replacement. One of the first ones replaces a space with a dash. Change it to use an underscore instead. Save the file.
Now force umbraco to recalculate all the site's urls by right-clicking each "level 1" node in the content tree. Select the 'Publish' menu and tick the Include Children box. Repeat for all the top-most nodes in your content tree. (note: using the 'republish entire site' will not recalculate the urls... you need the 'publish' menu for this).
Or, if you want to keep most of the urls using dashes and only a few need to use underscores, create a docType property with an alias of 'umbracoUrlName'. Whatever you put in that field will over-ride the default NiceUrl that umbraco would normally use for that page.
cheers,
doug.
it is so darn hard to get karma in this place with all you post answering ninjas!
what up Doc? :P
That's a great suggestion, Douglas. However, I'd still like to know how to do it with URLRewriting. ;-)
@baty-barr LOL I hear that man!
URL rewriting uses a regex pattern so you could replace all underscores in incoming requests to dashes for the pages in umbraco. The url rewriter documentation is excellent. You make all edits in the /config/urlrewriting.config file (which also references the docs and gives a link).
Shout if you have trouble coming up with the proper rule.
cheers,
doug.
Douglas, yes, that's it: I can't come up with the proper regex rule. I suck at regex, it totally mystifies me!
There I go shooting my mouth off thinking it would be easy... but when I tried it... I couldn't figure it out either!
Perhaps someone else can do a global replace of all "_" characters in the incoming url with "-" characters in the destination url but I can't crack this nut; at least not with the URLrewritingNet.UrlRewriter.
There are, of course, other rewriting tools you can use on your site and some of those would be able to the job I think.
Or, maybe reconsider some of the other options we'd discussed above?
cheers,
doug.
hey, i did find this article that might give a clue to the regex you might be looking for.
http://stackoverflow.com/questions/1279681/modrewrite-replace-underscores-with-dashes
I think something like this should work:
Oops, sorry, forgot to swap the right hand side to underscores. Try this:
Just noticed, you will also need to give each rule a unique name attribute aswell
Genius. Sheer GENIUS! That's awesome, Matt.
One note and one question for you, though.
Note: I think the goal is to go from urls that have underscores from the old site and change them to use dashes in umbraco. Therefore I think you want to change the rewrite rules to go the other way... from underscore to dashes.
Question: You could end up with a lot of these entries to handle an arbitrary number of underscores/dashes. Is there any way to do a greedy replace? (I suspect not but it would be great it were possible!)
Thanks again, Matt. I know who I'll be contacting when I need any rewrite or regex rules in the future ;)
cheers,
doug.
Ooops, thats what I get for rushing. You are correct, the hyphens and underscores should be as your example.
I can't say I have come across anything that would do it in one statement, but I like to keep my regex' as simple and readable as I can, so I would personaly prefer several readable rules, rather than one over complex rule. But I guess that also depends on how long you expect your URLs to be.
Matt
Saying that, another option might be to write a custom rewrite provider to do a recursive replace. Checkout the docs for an example.
Matt
Cool, thanks guys! That was very helpful!
You could also have gone about this the other way, and have umbraco replace all spaces by underscores instead of dashes, this behavior is defined in a config file:
/config/umbracoSettings.config
I've a question about the example of Doug/Matt. I want to use the rewrites, but replace + instead of _.
So I replaced the underscores with +'s, but there is no result. Can anyone help me on this one?
Thanks,
Niels
Hey guys, is someone know how to manage the same issue on newer Umbraco version? is there a better solution now instead of using regex redirect? thanks.
You could try looking into both IUrlProvider and iContentFinder. I've been looking into these and there are plenty of excellent resources online.
Two of the really good starting points are:
for
IContentFinder
:https://24days.in/umbraco-cms/2014/the-double-album/multi-site-404/
for
IUrlProvider
:https://our.umbraco.org/forum/developers/api-questions/49059-Custom-IUrlSegmentProvider-not-hit
A quick search through the forums will give you more info, but have a look here in the documentation as well:
https://our.umbraco.org/documentation/reference/routing/request-pipeline/outbound-pipeline
is working on a reply...