Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Robert J. Bullock 386 posts 405 karma points
    Mar 12, 2010 @ 21:32
    Robert J. Bullock
    0

    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.

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Mar 12, 2010 @ 21:49
    Douglas Robar
    4

    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.

  • bob baty-barr 1180 posts 1294 karma points MVP
    Mar 13, 2010 @ 04:53
    bob baty-barr
    1

    it is so darn hard to get karma in this place with all you post answering ninjas!

    what up Doc? :P

  • Robert J. Bullock 386 posts 405 karma points
    Mar 13, 2010 @ 15:42
    Robert J. Bullock
    0

    That's a great suggestion, Douglas. However, I'd still like to know how to do it with URLRewriting. ;-) 

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Mar 13, 2010 @ 16:53
    Nik Wahlberg
    0

    @baty-barr LOL I hear that man! 

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Mar 13, 2010 @ 17:12
    Douglas Robar
    0

    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.

  • Robert J. Bullock 386 posts 405 karma points
    Mar 15, 2010 @ 15:47
    Robert J. Bullock
    0

    Douglas, yes, that's it: I can't come up with the proper regex rule. I suck at regex, it totally mystifies me!

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Mar 15, 2010 @ 21:22
    Douglas Robar
    0

    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.

  • bob baty-barr 1180 posts 1294 karma points MVP
    Mar 16, 2010 @ 04:03
    bob baty-barr
    0

    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

     

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Mar 16, 2010 @ 09:47
    Matt Brailsford
    0

    I think something like this should work:

    <add virtualUrl="^([^-]*)-([^-]*)-([^-]*)-([^-]*)-(.*)$" destinationUrl="$1-$2-$3-$4-$5" rewriteUrlParameter="ExcludeFromClientQueryString" ignoreCase="true" />
    <add virtualUrl="^([^-]*)-([^-]*)-([^-]*)-(.*)$" destinationUrl="$1-$2-$3-$4" rewriteUrlParameter="ExcludeFromClientQueryString" ignoreCase="true" />
    <add virtualUrl="^([^-]*)-([^-]*)-(.*)$" destinationUrl="$1-$2-$3" rewriteUrlParameter="ExcludeFromClientQueryString" ignoreCase="true" />
    <add virtualUrl="^([^-]*)-(.*)$" destinationUrl="$1-$2" rewriteUrlParameter="ExcludeFromClientQueryString" ignoreCase="true" />
  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Mar 16, 2010 @ 09:48
    Matt Brailsford
    0

    Oops, sorry, forgot to swap the right hand side to underscores. Try this:

    <add virtualUrl="^([^-]*)-([^-]*)-([^-]*)-([^-]*)-(.*)$" destinationUrl="$1_$2_$3_$4_$5" rewriteUrlParameter="ExcludeFromClientQueryString" ignoreCase="true" />
    <add virtualUrl="^([^-]*)-([^-]*)-([^-]*)-(.*)$" destinationUrl="$1_$2_$3_$4" rewriteUrlParameter="ExcludeFromClientQueryString" ignoreCase="true" />
    <add virtualUrl="^([^-]*)-([^-]*)-(.*)$" destinationUrl="$1_$2_$3" rewriteUrlParameter="ExcludeFromClientQueryString" ignoreCase="true" />
    <add virtualUrl="^([^-]*)-(.*)$" destinationUrl="$1_$2" rewriteUrlParameter="ExcludeFromClientQueryString" ignoreCase="true" />
  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Mar 16, 2010 @ 09:51
    Matt Brailsford
    0

    Just noticed, you will also need to give each rule a unique name attribute aswell

    <add name

     

     

    ="UnderscoreRewrite1" ... />

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Mar 16, 2010 @ 11:01
    Douglas Robar
    0

    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.

    <add name="underscore2dash_04" virtualUrl="^([^-]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$" destinationUrl="$1-$2-$3-$4-$5" rewriteUrlParameter="ExcludeFromClientQueryString" ignoreCase="true" />
    <add name="underscore2dash_03" virtualUrl="^([^_]*)_([^_]*)_([^_]*)_(.*)$" destinationUrl="$1-$2-$3-$4" rewriteUrlParameter="ExcludeFromClientQueryString" ignoreCase="true" />
    <add name="underscore2dash_02" virtualUrl="^([^_]*)_([^_]*)_(.*)$" destinationUrl="$1-$2-$3" rewriteUrlParameter="ExcludeFromClientQueryString" ignoreCase="true" />
    <add name="underscore2dash_01" virtualUrl="^([^_]*)_(.*)$" destinationUrl="$1-$2" rewriteUrlParameter="ExcludeFromClientQueryString" ignoreCase="true" />

    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.

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Mar 16, 2010 @ 11:13
    Matt Brailsford
    0

    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

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Mar 16, 2010 @ 11:32
    Matt Brailsford
    0

    Saying that, another option might be to write a custom rewrite provider to do a recursive replace. Checkout the docs for an example.

    Matt

  • Robert J. Bullock 386 posts 405 karma points
    Mar 18, 2010 @ 15:42
    Robert J. Bullock
    0

    Cool, thanks guys! That was very helpful!

  • Rik Helsen 670 posts 873 karma points
    Mar 18, 2010 @ 16:55
    Rik Helsen
    100

    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

     <requestHandler>
        <!-- this will ensure that urls are unique when running with multiple root nodes -->
        <useDomainPrefixes>false</useDomainPrefixes>
        <urlReplacing>
          <char org=" ">-</char>
          <char org="&quot;"></char>
          <char org="."></char>
          <char org=";"></char>
          <char org="/"></char>
          <char org=":"></char>
          <char org="’">-</char>
          <char org="'">-</char>
          <char org="é">e</char>
          <char org="è">e</char>
          <char org="à">a</char>
          <char org="ë">e</char>
          <char org="ç">c</char>
          <char org="+">plus</char>
          <char org="*">star</char>
          <char org="&amp;">-</char>
          <char org="?"></char>
          <char org="æ">ae</char>
          <char org="ø">oe</char>
          <char org="å">aa</char>
          <char org="ä">ae</char>
          <char org="ö">oe</char>
          <char org="ü">ue</char>
          <char org="ß">ss</char>
          <char org="Ä">ae</char>
          <char org="Ö">oe</char>
        </urlReplacing>
      </requestHandler>
  • Niels 63 posts 119 karma points
    Aug 01, 2013 @ 11:07
    Niels
    0

    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

  • Nonacc 63 posts 184 karma points
    Jun 04, 2017 @ 19:31
    Nonacc
    0

    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.

  • MuirisOG 382 posts 1284 karma points
    Jun 05, 2017 @ 10:51
    MuirisOG
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft