1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Redirect to homepage if language page not exists

Redirect to homepage if language page not exists

2 posts, 0 answered
  1. medo
    medo avatar
    9 posts
    Registered:
    22 Nov 2009
    14 Jan 2010
    Link to this post
    Hi,

    I have a multilingual site.
    The switching is done with a custom languagelist selector using images of countryflags.
    The code is based on the sample provided here.
    http://www.sitefinity.com/support/forums/sitefinity-3-x/developing-with-sitefinity/multilingual-showing-language-links-instead-of-drop-down.aspx

    For the default language I have a Page in dutch. (lets call it AboutUs.aspx)
    De english site does not have this page.
    When I now click in the english flag I get
    /en/AboutUs.aspx but as this page doesn't exist I get the same Dutch content, which is weird in my opinion.
    What I want to achieve is that if the page doesn't exist I get the english Homepage.
    How can I achieve this?

    regards.
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    15 Jan 2010
    Link to this post
    Hi medo,

    You can add the following code as a user control to your template. It will redirect all requests to a not existing pages to your default home page.

    protected override void OnPreRender(EventArgs e)
       {
           base.OnPreRender(e);
           if (!Page.IsPostBack)
           {
               Guid pageID = new Guid(this.Context.Items["cmspageid"].ToString());
               IList<string> stringList = new List<string>();
               var manager = new CmsManager();
               ICmsPage page = (ICmsPage)manager.GetPage(pageID, true);
               if (page != null)
               {
                   foreach (IPageContent version in page.LanguageVersions.Values)
                   {
                       int cult = version.LangID;
                       CultureInfo pageCult = new CultureInfo(cult);
                       string cu = pageCult.Name;
                       stringList.Add(cu);
                   }
                   CultureInfo culture = CultureInfo.GetCultureInfo(page.LangID);
                   string c = culture.Name;
                   foreach (string st in stringList)
                   {
                       
                       if (st != c && !String.IsNullOrEmpty(c))
                       {
                           Response.Redirect("http://www.domain/home.aspx");
                           
                       }
                   }
               }
     
           }
       }


    You could also implement the logic in custom HttpHanlder.

    Regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
2 posts, 0 answered