November Happy Hour will be moved to Thursday December 5th.

Shortened URL's

Vote:
 

Hi All,

Is there a way in EPiServer (or another method anyone knows about) to change a URL such as http://www.sitename.com/en/I-am-a-page-with-a-really-long-name/I-am-a-page-with-a-really-long-name/ to www.sitename.com/shorturl

Many Thanks.

#59847
Jul 02, 2012 15:14
Vote:
 

Override and implement your own friendly URL provider and add the logic there. I've simply used the pageid for this previously. Eg: www.sitename.com/212.

Hope this helps.

Frederik

#59848
Jul 02, 2012 15:33
Vote:
 

Hi Frederik,

Which URL provider do you use? Any more help would be great - talking to a coding novice to be honest!

Thanks.

#59849
Jul 02, 2012 15:40
Vote:
 

Something like (using page id):

using System.Web;
using EPiServer;
using EPiServer.Core;

namespace xxxx.UrlRewrite
{
    public class FriendlyUrlRewriteProvider : EPiServer.Web.FriendlyUrlRewriteProvider
    {
        public string RedirectUrl { get; set; }

        public override bool TryConvertToInternal(EPiServer.UrlBuilder url, out System.Globalization.CultureInfo preferredCulture, out object internalObject)
        {
            //Use the default rewriting behavior
            if (base.TryConvertToInternal(url, out preferredCulture, out internalObject))
            {
                return true;
            }

            HttpContext context = HttpContext.Current;

            if (IsValidShortEPiServerUrl(context))
            {
                PerformRedirect(context);
            }

            return false;
        }

        private bool IsValidShortEPiServerUrl(HttpContext context)
        {
            string path = context.Request.Url.Segments[1];

            int pageId;

            if (int.TryParse(path, out pageId))
            {
                var pageReference = new PageReference(pageId);

                try
                {
                    PageData destinationPage = DataFactory.Instance.GetPage(pageReference);

                    SetRedirectUrl(destinationPage);

                    return true;
                }
                catch (PageNotFoundException ex)
                {
                    return false;
                }
            }

            return false;
        }

        private void SetRedirectUrl(PageData destinationPage)
        {
            RedirectUrl = destinationPage.LinkURL;
        }

        private void PerformRedirect(HttpContext context)
        {
            context.ClearError();

            string url = RedirectUrl;

            context.Response.Clear();
            context.Response.StatusDescription = "Moved Permanently";
            context.Response.StatusCode = 301;
            context.Response.RedirectLocation = url;
            context.Response.End();
        }
    }
}

    Then you need to update the default URL provider used in episerver.config:

<urlRewrite defaultProvider="CustomFriendlyUrlRewriteProvider">
    <providers>
      <add name="EPiServerFriendlyUrlRewriteProvider" type="EPiServer.Web.FriendlyUrlRewriteProvider,EPiServer" />
      <add name="CustomFriendlyUrlRewriteProvider" type="xxxx.UrlRewrite.FriendlyUrlRewriteProvider, xxxx" />
      <add description="EPiServer identity URL rewriter" name="EPiServerIdentityUrlRewriteProvider" type="EPiServer.Web.IdentityUrlRewriteProvider,EPiServer" />
      <add description="EPiServer bypass URL rewriter" name="EPiServerNullUrlRewriteProvider" type="EPiServer.Web.NullUrlRewriteProvider,EPiServer" />
    </providers>
  </urlRewrite>

   

Frederik

#59850
Jul 02, 2012 16:02
Vote:
 

Marc, if it's only a few pages that you want to shorten, then you can use "Simple address for this page" under the "Settings" tab. If you want to shorten all pages, then Frederiks approach might be handier. :)

#59853
Jul 02, 2012 16:10
Vote:
 

Thanking you both!

#59873
Jul 03, 2012 10:55
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.