Search code examples
url-rewritingsitecoresitecore7

how to rewrite or shorten the Sitecore URL


I have Sitecore website with URL http://www.abc.com/our-resorts/resort1/career.aspx But I want to shorten the URL like http://www.abc.com/resort1/career.aspx . Any idea how to remove our-resorts from the URL.

enter image description here

Please any one from Sitecore community help me ....

thanks


Solution

  • The first answer that comes to my mind is to use custom LinkProvider (to generate the urls on the site) and custom ItemResolver (to resolve items when shortened url comes).

    1. Create your own class MyLinkProvider inheriting from Sitecore.Links.LinkProvider and override GetItemUrl method. If the url contains /our-resorts/, remove our-resorts. Of course this assumes that you don't have out items with the name our-resorts which should not be omitted in link generation. Replace in Sitecore.config standard LinkProvider with the new one.
    2. Create MyItemResolver class inheriting from HttpRequestProcessor and add it to the <httpRequestBegin> pipeline directly after ItemResolver. Code for the new item resolver:
    public class ItemResolver : HttpRequestProcessor
    {
        public override void Process(HttpRequestArgs args)
        {
            if (Context.Item != null || Context.Database == null || args.Url.ItemPath.Length == 0)
                return;
            string path = "/our-resorts" + MainUtil.DecodeName(args.Url.ItemPath);
            Context.Item =  args.GetItem(path);
        }
    }
    

    I haven't compiled or tested the code so you need to test it on your own. If you have any more questions, post them below.