What is Aliases in Sitecore

Sitecore determines the default URL for an item based on its path; for instance the default URL for /sitecore/content/home/hr/jobs is /hr/jobs.aspx.  In some cases it is useful to have shorter URLs which map to longer paths, for instance /jobs.aspx may be preferable for marketing materials (email campaigns, print advertisements, etc.).  Sitecore supports alternate URLs through a feature known as aliases.

Creating Alias to an item

It is very simple to create an Alias for the Item. All you have to do is, log on to Sitecore admin control panel. In the Sitecore tree, go to System -> Aliases, and click on the Alias in the options section. Provide the name of the alias as shown in the below screen capture. Click on the Ok button to save the alias.

1-sitecore-setup-alias

Once the alias is saved, you can see the Sample alias under Aliases folder. Click on the Sample item to see the properties. In the properties section, go to Data section and click on the Insert link button.

2-sitecore-setup-alias-link

In the insert link popup window, select the item you want to create an alias for, in this case, I am selecting the item Home -> Sample Item -> Sample Item as shown in the below screen capture and click on the Insert button on the popup.

3-sitecore-setup-alias-link-insertlinkpopup

Finally save all the changes.

That’s all. You have created an alias Sample for the content item Home -> Sample Item -> Sample Item.

  • Sitecore first checks the aliases before trying to render the page.
  • If you have multiple sites hosted on a single Sitecore instance, remember, the same alias is applicable to all the websites.
  • The same content will be served with two different URLs, I.e. the original item URL and the alias. In the above case, you can browse the page with item URL /Home/Sample Item/Sample Item and /Sample. This might sometimes cause confusion for search engines to index the pages. Someone might use the original item URL and some other might use the alias, google will index both the pages and find duplicate content which might have impact on the page rank.

Implementing Aliases in Multi-Site environment

In multisite environment, we can still use this functionality. We can customize the AliasResolverprocessor in HttpRequestBegin pipeline, where the alias items will be stored in Site specific folder, so, the Custom AliasResolver will search aliases in site folder only. We can create a alias folder structure site-wise, something like,

/Sitecore/System/Aliases/Site-A/Alias1
/Sitecore/System/Aliases/Site-A/Alias2
/Sitecore/System/Aliases/Site-B/Alias1
/Sitecore/System/Aliases/Site-B/Alias2

Multi-Alias

This structure might change depending on what folder structure you follow. See how code will go for Custom AliasResolver:

namespaceSitecoreTactics.Pipelines.HttpRequestBegin
{
classCustomAliasResolver : AliasResolver
{
    publicnewvoidProcess(HttpRequestArgs args)
    {
        Assert.ArgumentNotNull(args, "args");
        if(!Settings.AliasesActive)
        {
            Tracer.Warning("Aliases are not active.");
        }
        else
        {
            Sitecore.Data.Database database = Sitecore.Context.Database;
            if(database == null)
            {
                Tracer.Warning("There is no context database in AliasResover.");
            }
            Item aliasItem = getAliasItem(args);
            if(aliasItem != null)
            {
                LinkField linkField = aliasItem.Fields["Linked item"];
                if(linkField != null)
                {
                    Item AliasLinkedTo = Sitecore.Context.Database.GetItem(linkField.TargetID);
                    if(AliasLinkedTo != null)
                    {
                        Sitecore.Context.Item = AliasLinkedTo;
                    }
                }
                else
                {
                    base.Process(args);
                }
            }
        }           
    }
    ///
    /// Gets the alias item.

    ///

    ///The args.
    ///
    privateItem getAliasItem(HttpRequestArgs args)
    {
        stringsiteName = Sitecore.Context.Site.RootPath.ToLower();
        if(args.LocalPath.Length > 1)
        {
            Item aliasItem = Sitecore.Context.Database.GetItem("/sitecore/system/Aliases/"+ siteName + "/"+ args.LocalPath);
            if(aliasItem != null)
            {
                returnaliasItem;
            }               
        }
        returnnull;
    }
}
}

Sitecore Multilingual Item Alias

Sitecore by default provides Item Aliases for single language. Here we are going to check that when a request come for an alias, the content of the Linked Item should come with selected language.

Setps

  1. In the Alias template (/sitecore/templates/System/Alias), add a new field Linked Language with DropLink field.
    So, when a request comes like http://domain/idioma/, it should serve the Products page with es-ES language. Linked-Language
  2. Override the AliasResolver like below:
    namespace SitecoreTactics.HttpRequestPipeline
    {
        public class MultilingualAliasResolver : HttpRequestProcessor
        {
            public override void Process(HttpRequestArgs args)
            {
                Assert.ArgumentNotNull((object)args, "args");
                if (!Settings.AliasesActive)
                {
                    Tracer.Warning((object)"Aliases are not active.");
                }
                else
                {
                    Database database = Context.Database;
                    if (database == null)
                    {
                        Tracer.Warning((object)"There is no context database in AliasResover.");
                    }
                    else
                    {
                        Profiler.StartOperation("Resolve alias.");
                        if (database.Aliases.Exists(args.LocalPath) && !this.ProcessItem(args))
                            this.ProcessExternalUrl(args);
                        Profiler.EndOperation();
                    }
                }
            }
            private void ProcessExternalUrl(HttpRequestArgs args)
            {
                string targetUrl = Context.Database.Aliases.GetTargetUrl(args.LocalPath);
                if (targetUrl.Length <= 0)
                    return;
                this.ProcessExternalUrl(targetUrl);
            }
            private void ProcessExternalUrl(string path)
            {
                if (Context.Page.FilePath.Length > 0)
                    return;
                Context.Page.FilePath = path;
            }
            private bool ProcessItem(HttpRequestArgs args)
            {
                bool aliasFound = false;
                string alias = args.LocalPath;
                if (alias.Length > 0)
                {
                    Item obj = ItemManager.GetItem(FileUtil.MakePath("/sitecore/system/aliases", alias, '/'), Language.Invariant,
                        Version.First, Sitecore.Context.Database, SecurityCheck.Disable);
                    if (obj != null)
                    {
                        LinkField itemField = (LinkField)obj.Fields["linked item"];
                        string language = obj["linked language"];
                        if (!string.IsNullOrEmpty(language) && itemField!= null)
                        {
                            Item langItem = Sitecore.Context.Database.GetItem(new ID(language));
                            if (langItem != null)
                            {
                                Language lang = Language.Parse(langItem.Name);
                                Item item = Sitecore.Context.Database.GetItem(itemField.TargetID, lang);
                                if (item != null)
                                {
                                    this.ProcessItem(args, item);
                                    aliasFound = true;
                                }
                            }
                        }
                        else if (itemField!=null)
                        {
                            Item item = Sitecore.Context.Database.GetItem(itemField.TargetID);
                            if (item != null)
                            {
                                this.ProcessItem(args, item);
                                aliasFound = true;
                            }
                        }
                    }
                }
                return aliasFound;
            }
            private void ProcessItem(HttpRequestArgs args, Item target)
            {
                if (Context.Item != null)
                    return;
                Context.Item = target;
                Context.Language = target.Language;
            }
        }
    }
  3. In Web.config, replace the Sitecore’s AliasResolver and add our custom overridden MultilingualAliasResolver as below:
    <!--<processor type="Sitecore.Pipelines.HttpRequest.AliasResolver, Sitecore.Kernel" />-->
    <processortype="SitecoreTactics.HttpRequestPipeline.MultilingualAliasResolver, SitecoreTactics"/>

Note: Above code works only for Linked Items which are Sitecore Internal Links.

Disabling Aliases

If you do not use aliases, you can disable aliases by setting the AliasesActive setting
in the Web.config file to false, or by removing the AliasResolver processor from the
httpRequestBegin pipeline.

4220cookie-checkWhat is Aliases in Sitecore

Add a Comment

Your email address will not be published. Required fields are marked *