Try our conversational search powered by Generative AI!

Rewriting Friendly Url requests to Full Url

Vote:
 

Hi,

We are migrating to episerver 5.0 R2 and wre came across the following issue.

We have a episeserver page UKContactUsPage under tree structure : SiteName/ContactUs/Country/UKContactUsPage

i.e : Simple Address for the page : UKContactUsPage

A simple address (Friendly URL) is set for the page as : UK

When we browse to the page as http://sitename/UK , it should redirect the user to the full address (i.e http://sitename/SiteName/ContactUs/Country/UKContactUsPage)

The page is served as expected, which is good, but the url still doesnt rewrite to full address (http://sitename/SiteName/ContactUs/Country/UKContactUsPage).

This was not the case when we had episerver 4.62. The user would be redirected to full address. We want to retain this feature as we wouldnt want duplicate URLs for SEO reasons.

How can I achieve this in EPiServer 5.  Am I missing any setting here. Is this the default behavior in EPiServer 5 and 6?

Kindly help

 

 

#54802
Nov 03, 2011 13:37
Vote:
 
This is by default in EPiServer 5 and 6. 
Put this piece of code in Global.asax.cs
protected void Application_Start(Object sender, EventArgs e)
{
EPiServer.Web.InitializationModule.FirstBeginRequest += new EventHandler(InitializationModule_FirstBeginRequest);
}
void InitializationModule_FirstBeginRequest(object sender, EventArgs e)
{
EPiServer.Global.UrlRewriteProvider.ConvertedToExternal += new EventHandler<EPiServer.Web.UrlRewriteEventArgs>(UrlRewriteProvider_ConvertedToInternal);
}
void UrlRewriteProvider_ConvertedToInternal(object sender, EPiServer.Web.UrlRewriteEventArgs e)
{
//Redirect to real page if permalink if it exists
if (e.IsModified)
{
PageReference pageReference = (PageReference)e.Internal;
PageData page = DataFactory.Instance.GetPage(pageReference);
if (page["PageExternalURL"] != null && ((string)page["PageExternalURL"]) == HttpContext.Current.Request.Url.LocalPath.Replace("/", string.Empty) &&
GetExternalUrl(page.LinkURL) != HttpContext.Current.Request.Url.LocalPath)
{
HttpContext.Current.Response.Status = "301 Moved Permanently";
HttpContext.Current.Response.AddHeader("Location", Helper.Instance.GetExternalUrl(page.LinkURL) + HttpContext.Current.Request.Url.Query);
}
}
}

  

 public string GetExternalUrl(string internalUrl)
        {
            UrlBuilder ub = new EPiServer.UrlBuilder(internalUrl);
            Global.UrlRewriteProvider.ConvertToExternal(ub, null, System.Text.Encoding.Default);

            return ub.ToString();
        }

    

#54860
Edited, Nov 07, 2011 13:23
Vote:
 

Hello Erik,

Thanks a lot for your time and reply and clarifying my doubt.

In the code snippet, I could only see the method for UrlRewriteProvider_ConvertedToInternal but not the UrlRewriteProvider_ConvertedToExternal. Can you please let me know if the method is correct.

Based on your hint, I tried to write on my own, but couldn't succeed. I get into infinite loop.

Can you please help? 

 

Can you please help?


#54894
Nov 08, 2011 18:51
Vote:
 

I have edited the code to fix the typo with Internal/External. I also added a method GetExternalUrl, which I forgot to type here.

#54961
Nov 11, 2011 14:01
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.