Copied to clipboard

Flag this post as spam?

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


  • Jeroen Breuer 4909 posts 12266 karma points MVP 5x admin c-trib
    Feb 29, 2016 @ 14:50
    Jeroen Breuer
    1

    URL rewriting

    Hello,

    I see this package uses the UrlRewriting.config file. You can also do this with a UrlProvider and ContentFinder.

    Some more info: http://24days.in/umbraco/2014/urlprovider-and-contentfinder/

    I've created an example which uses 1-1 multilingual. So a single node can have different URLs per language.

    You can find more info here: https://our.umbraco.org/projects/developer-tools/1-1-multilingual-example/

    Jeroen

  • dimi309 245 posts 579 karma points
    Feb 29, 2016 @ 19:41
    dimi309
    0

    Thanks! I'll have a look...

  • Nicholas Westby 2054 posts 7104 karma points c-trib
    Feb 29, 2016 @ 21:08
    Nicholas Westby
    2

    While I'm not using the content finder (I'm using URL rewriting), I am using a custom URL provider so I don't have to worry about prefixing all of my URL's with "/en-us". Here's the implementation if you are curious:

    // Namespaces.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Umbraco.Core.Configuration;
    using Umbraco.Core.Logging;
    using Umbraco.Web;
    using Umbraco.Web.Routing;
    
    
    /// <summary>
    /// Builds international URL's.
    /// </summary>
    public class InternationalUrlProvider : DefaultUrlProvider
    {
    
        #region Constants
    
        private const string ErrorBuildingUrl = "Error while attempting to build international URL.";
    
        #endregion
    
    
        #region Constructors
    
        /// <summary>
        /// Default constructor.
        /// </summary>
        public InternationalUrlProvider() : base(UmbracoConfig.For.UmbracoSettings().RequestHandler)
        {
        }
    
        #endregion
    
    
        #region Public Methods
    
        /// <summary>
        /// Gets the URL's for other domains.
        /// </summary>
        public override IEnumerable<string> GetOtherUrls(UmbracoContext umbracoContext, int id, Uri current)
        {
            var urls = base.GetOtherUrls(umbracoContext, id, current);
            return urls == null
                ? null
                : urls.Select(x => InternationalizeUrl(x, umbracoContext)).ToList();
        }
    
    
        /// <summary>
        /// Gets the main URL.
        /// </summary>
        public override string GetUrl(UmbracoContext umbracoContext, int id, Uri current, UrlProviderMode mode)
        {
            var url = base.GetUrl(umbracoContext, id, current, mode);
            return InternationalizeUrl(url, umbracoContext);
        }
    
        #endregion
    
    
        #region Private Methods
    
        /// <summary>
        /// Internationalizes a URL.
        /// </summary>
        private string InternationalizeUrl(string url, UmbracoContext umbracoContext)
        {
    
            // Log errors.
            try
            {
                var validRequest = umbracoContext != null && umbracoContext.HttpContext != null
                    && umbracoContext.HttpContext.Request != null;
                var request = validRequest ? umbracoContext.HttpContext.Request : null;
                var lang = validRequest ? request.QueryString["lang"] : "en-us";
                if (string.IsNullOrWhiteSpace(lang))
                {
                    lang = "en-us";
                }
                var baseUri = default(Uri);
                var validBase = false;
                try
                {
                    baseUri = new Uri(url);
                    validBase = true;
                }
                catch
                {
                    baseUri = new Uri("http://www.site.com/");
                }
                var builder = new UriBuilder(new Uri(baseUri, url));
                var path = builder.Path.TrimStart("/".ToCharArray());
                var langPart = lang + (string.IsNullOrWhiteSpace(path) ? null : "/");
                builder.Path = "/" + langPart + path;
                url = validBase ? builder.Uri.AbsoluteUri : builder.Uri.PathAndQuery;
            }
            catch (Exception ex)
            {
                LogHelper.Error<InternationalUrlProvider>(ErrorBuildingUrl, ex);
            }
    
    
            // Return URL.
            return url;
    
        }
    
        #endregion
    
    }
    

    And here's how I register that URL provider:

    // Namespaces.
    using Routing;
    using Umbraco.Core;
    using Umbraco.Web.Routing;
    
    
    /// <summary>
    /// Handles application startup.
    /// </summary>
    public class MyAppStart : ApplicationEventHandler
    {
    
        #region Methods
    
        /// <summary>
        /// Application starting.
        /// </summary>
        protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication,
            ApplicationContext applicationContext)
        {
    
            // URL provider for international URL's.
            UrlProviderResolver.Current.RemoveType<DefaultUrlProvider>();
            UrlProviderResolver.Current.InsertType<InternationalUrlProvider>();
    
    
            // Boilerplate.
            base.ApplicationStarting(umbracoApplication, applicationContext);
    
        }
    
        #endregion
    
    }
    

    As I mentioned, I'm not yet using a content finder, so this is what my URL rewrites look like:

    <?xml version="1.0" encoding="utf-8"?>
    <urlrewritingnet xmlns="http://www.urlrewriting.net/schemas/config/2006/07">
      <rewrites>
    
        <!-- Rewrite international URL's. -->
        <add
          name="Localize URL (With QS)" rewriteUrlParameter="IncludeQueryStringForRewrite" ignoreCase="true"
          virtualUrl="^~/(?&lt;culture&gt;[a-z]{2}-[a-z]{2})(?&lt;url&gt;/((?!\?).)+\?.*|/?\?.*)$"
          destinationUrl="~${url}&amp;lang=${culture}" />
        <add
          name="Localize URL (No QS)" rewriteUrlParameter="IncludeQueryStringForRewrite" ignoreCase="true"
          virtualUrl="^~/(?&lt;culture&gt;[a-z]{2}-[a-z]{2})(?&lt;url&gt;/((?!\?).)+|)$"
          destinationUrl="~${url}?lang=${culture}" />
    
      </rewrites>
    </urlrewritingnet>
    
  • dimi309 245 posts 579 karma points
    Mar 01, 2016 @ 08:23
    dimi309
    0

    That's a pretty good idea!

  • dimi309 245 posts 579 karma points
    Mar 01, 2016 @ 08:25
    dimi309
    0

    When I was using Umbraco myself, I was doing it this way: http://dimitros.be/en/blog/betterseowithpolyglot/

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies