Copied to clipboard

Flag this post as spam?

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


  • David Zweben 268 posts 753 karma points
    Feb 08, 2018 @ 16:08
    David Zweben
    0

    Help with URL Rewriting?

    Hi,

    I have a site that has URLs in the following format:

    www.example.com/path/subpath/translations/es/
    www.example.com/path/subpath/translations/de/
    www.example.com/path/subpath/translations/ja/
    

    I am looking to change these URLs to the following format without rearranging the actual nodes or doing manual per-page URL changes, presumably using URL rewriting:

    www.example.com/es/path/subpath/
    www.example.com/de/path/subpath/
    www.example.com/ja/path/subpath/
    

    In this scenario, it's safe to assume that the word "translations" and the language abbreviations won't be used elsewhere in the URLs.

    Unfortunately, though, I am not familiar with regular expressions. If someone could show me how to do this, or at least point me in the right direction, I'd really appreciate it.

    Thanks, David

  • Anders Bjerner 487 posts 2989 karma points MVP 8x admin c-trib
    Feb 08, 2018 @ 16:38
    Anders Bjerner
    0

    Try something like this:

    <rule name="Our" stopProcessing="true">
      <match url="([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/translations/([a-z]{2})/$" />
      <action type="Redirect" redirectType="Permanent" url="{R:3}/{R:1}/{R:2}/" />
    </rule>
    

    The example uses regex to match the incoming URLs. It assumes that both path and subpath only consists of alphanumeric characters and/or hyphens.

    It creates a permanent redirect to the "new" URL. If the user should keep seeing the "old" URL, you can change the type parameter to Rewrite instead of Redirect.

    Hope that helps ;)

  • David Zweben 268 posts 753 karma points
    Feb 08, 2018 @ 17:03
    David Zweben
    0

    Hi Anders,

    Thanks so much for the reply.

    This looks like it would only work for URLs that have exactly two levels, though. How could it be modified so it will work with any number of path levels before "/translations"?

    In other words, it would help if it could transform:

    www.example.com/any/number/of/path/parts/translations/es/
    

    to:

     www.example.com/es/any/number/of/path/parts/
    

    Any ideas?

    Thanks,
    David

  • Anders Bjerner 487 posts 2989 karma points MVP 8x admin c-trib
    Feb 08, 2018 @ 17:06
    Anders Bjerner
    0

    Yes. I wasn't sure whether you needed that. We can make the regex less specific:

    <rule name="Our" stopProcessing="true">
        <match url="(.+?)/translations/([a-z]{2})/$" />
        <action type="Redirect" redirectType="Permanent" url="{R:2}/{R:1}/" />
    </rule>
    

    It works with your example ;)

  • David Zweben 268 posts 753 karma points
    Feb 08, 2018 @ 17:40
    David Zweben
    0

    Hi Anders,

    Thanks again for your help so far.

    So I have a real page on Umbraco whose native URL follows this format:

     www.example.com/path/subpath/translations/es/
    

    I added the second rule you provided to the Web.config file, and entered in the URL below:

     www.example.com/es/path/subpath/
    

    But I just get a 404 message. Any idea why this might be? Looking at the rule, it seems like the "/translations/" might be on the wrong side of the rule.

    Thanks,
    David

  • Anders Bjerner 487 posts 2989 karma points MVP 8x admin c-trib
    Feb 08, 2018 @ 18:04
    Anders Bjerner
    0

    Just to be sure, what address do you enter in the location bar, and what address do you see in the location bar when you get the 404?

    I've tried to test up a test solution with the rule I posted before, and then point my browser to:

    http://localhost:59797/path/subpath/translations/es/
    

    I'm then immediately redirected to:

    http://localhost:59797/es/path/subpath/
    

    A page must exist with this URL - otherwise you'll get the 404 page.

  • David Zweben 268 posts 753 karma points
    Feb 08, 2018 @ 18:08
    David Zweben
    0

    Hi Anders,

    Sorry for the confusion! I looked back and my original post was vague. I'm looking for it to work the other way around.

    I want to be able to enter in something like this to the URL bar:

     http://localhost:12345/es/path/subpath/
    

    ...and have it arrive at the Umbraco page with the default URL of:

     http://localhost:12345/path/subpath/languages/es/
    

    Is there any way this can be done? Really appreciate your help!

    Regards,
    David

  • Anders Bjerner 487 posts 2989 karma points MVP 8x admin c-trib
    Feb 08, 2018 @ 18:16
    Anders Bjerner
    1

    Should be possible as well - try something like:

    <rule name="Our" stopProcessing="true">
        <match url="^([a-z]{2})/(.+?)/$" />
        <action type="Redirect" url="{R:2}/translations/{R:1}/" />
    </rule>
    
  • Nik 1611 posts 7237 karma points MVP 7x c-trib
    Feb 08, 2018 @ 18:18
    Nik
    0

    Hi David,

    Just out of interest... are you expecting the url in the browser to change or are you want it to display the content from that page?

    If it's the latter, you might be wanting something a bit more complex than a re-direct type rule you are probably looking more a rewrite rule or even a custom contentfinder.

    Nik

  • Anders Bjerner 487 posts 2989 karma points MVP 8x admin c-trib
    Feb 08, 2018 @ 18:21
    Anders Bjerner
    2

    Yes, as I wrote earlier, the type="Redirect" attribute can be changed to type="Rewrite", and the URL in the location bar won't change.

    A content finder becomes slightly more complex, so I would stick with URL rewriting if possible.

  • Nik 1611 posts 7237 karma points MVP 7x c-trib
    Feb 08, 2018 @ 18:25
    Nik
    0

    Ahh, I missed that in your earlier post Anders :-)

  • David Zweben 268 posts 753 karma points
    Feb 08, 2018 @ 18:46
    David Zweben
    0

    Thanks so much Anders! The latest code you provided seems to work great, and the "Rewrite" attribute does just what I needed!

    I realized a complication, though. This will work great when the URL is typed in, but when Umbraco is outputting URLs via Razor or the APIs, it is still going to use the original URL.

    How could I have the URL that Umbraco stores natively also follow the new format? Would that require the use of a content finder?

    Thanks,
    David

  • Anders Bjerner 487 posts 2989 karma points MVP 8x admin c-trib
    Feb 08, 2018 @ 19:03
    Anders Bjerner
    100

    A content finder is for handling incoming URLs. Like Nik mentioned, you could have used an content finder instead of URL rewriting.

    To give a content item another URL, you can instead use a URL provider. This will let you control what the Url property returns. There's a bit documentation about URL providers here:

    https://our.umbraco.org/Documentation/Reference/Routing/Request-Pipeline/outbound-pipeline#urls

    Jeroen Breuer also wrote an article 24 days of Umbraco about URL providers:

    https://24days.in/umbraco-cms/2014/urlprovider-and-contentfinder/

    Keep in mind that URL providers only provides the visual URL. To make sure that Umbraco actually finds the correct content, you could use a content finder. Or just the URL rewriting like we've discussed in this thread.

  • David Zweben 268 posts 753 karma points
    Feb 08, 2018 @ 19:07
    David Zweben
    0

    Thanks!

    I'll try making a URL provider to go along with the URL rewrite rule, and if that doesn't work, I'll try switching out the URL rewrite for a content finder.

Please Sign in or register to post replies

Write your reply to:

Draft