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

Forums / Developing with Sitefinity / Localization and URL Rewritting

Localization and URL Rewritting

16 posts, 0 answered
  1. Joe
    Joe avatar
    16 posts
    Registered:
    10 May 2010
    24 Dec 2010
    Link to this post
    Hi,

    I would like to know whether there is a way to remove the culture code from URLs in the site without it having any effect on the Localization module itself.

    I thought about changing the persistenceMode to "QueryString" but if a page already has a query string the sflang would no longer be: "?sflang=en-GB" but & "?sflang=en-GB".

    Thanks,
    Matthew
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    24 Dec 2010
    Link to this post
    Hi Massimiliano,

    Yes there will be a key "&sflang=", but this should not break the localization.

    Regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Joe
    Joe avatar
    16 posts
    Registered:
    10 May 2010
    24 Dec 2010
    Link to this post
    What I mean is, can this key be removed with Advanced URL Rewriter?
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    24 Dec 2010
    Link to this post
    Hello Massimiliano,

    You can remove it but most probably this will break the behavior of Language control and page editor which check for the query string in the url.

    Regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Joe
    Joe avatar
    16 posts
    Registered:
    10 May 2010
    24 Dec 2010
    Link to this post
    So I have no way of changing both (example):

    www.sitename.com/en-GB/products.aspx?id=1
    and
    www.sitename.com/it-IT/products.aspx?id=1

    to

    www.sitename.com/products/product1

    ?

    Thanks
  6. Joe
    Joe avatar
    16 posts
    Registered:
    10 May 2010
    03 Jan 2011
    Link to this post
    Hi,

    Can I get a response on my latest post?

    Thanks
  7. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    03 Jan 2011
    Link to this post
    Hello Massimiliano Rizzuto,

    If you use such urls how would you detect which localization culture should be used? You need to pass the culture somehow so that when you rewrite the url you know to which language to rewrite.

    Regards,
    Radoslav Georgiev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  8. Joe
    Joe avatar
    16 posts
    Registered:
    10 May 2010
    04 Jan 2011
    Link to this post
    Hi Radoslav,

    The language is also kept in a cookie - but I understand what you mean. What I meant was, can't the rewritten URL not have the language code? It would still be in the actual requested URL, same as the query strings, no?

    Thanks
  9. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    04 Jan 2011
    Link to this post
    Hello Massimiliano Rizzuto,

    If you are persisting the language in a cookie what you can do to create an extension of the CmsHttpModuleUrlRewrite module to apply the language code on actual url depending on the value of the cookie.
    using System.Web;
    using Telerik.Cms.Web;
     
    /// <summary>
    /// Summary description for CustomCmsHttpModuleUrlRewrite
    /// </summary>
    public class CustomCmsHttpModuleUrlRewrite : CmsHttpModuleUrlRewrite
    {
        protected override string GetUrl(HttpContext context)
        {
            string url = context.Request.RawUrl;
            Telerik.Cms.Web.AdvancedUrlRewriter.IRewriteRule rule = AdvancedUrlRewriter.Rewriter.FindMatch(url);
            if (rule != null)
            {
                url = rule.Execute(url);
                //you will need to read value from the cookie here and
                //add the culture to the url variable
                if (rule.Mode == AdvancedUrlRewriter.RewriteMode.Rewrite)
                    return url;
     
                this.Redirect(url, (rule.Mode == AdvancedUrlRewriter.RewriteMode.PermanentRedirect));
            }
            return string.Empty;
        }
    }

    Then you have to substitute the use of the default module in web.config with yours.

    This way you will be able to intercept incoming urls such as www.sitename.com/products/product1 and rewrite them to the proper language.

    All the best,
    Radoslav Georgiev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  10. Joe
    Joe avatar
    16 posts
    Registered:
    10 May 2010
    05 Jan 2011
    Link to this post
    Hi Radoslav,

    Hopefully this will be my final question on this.

    I cannot find IRewriteRule nor RewriteMode inTelerik.Cms.Web.AdvancedUrlRewriter

    I enabled the module following this page:
    http://www.sitefinitywatch.com/blog/09-01-08/URL_Rewriting_with_Sitefinity_s_AdvancedUrlRewriter.aspx

    I also get the error 'Redirect is not a member of CustomCmsHttpModuleUrlRewrite'

    Could this be something in my web.config?
  11. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    05 Jan 2011
    Link to this post
    Hi Massimiliano Rizzuto,

    Problem should not be in web.config. Can you please let me know exactly which version of Sitefinity you are using?

    Greetings,
    Radoslav Georgiev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  12. Joe
    Joe avatar
    16 posts
    Registered:
    10 May 2010
    05 Jan 2011
    Link to this post
    3.7 sp2
  13. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    05 Jan 2011
    Link to this post
    Hi Massimiliano Rizzuto,

    Bellow is a code stub for your version of Sitefinity:
    public class CustomCmsHttpModuleUrlRewrite : CmsHttpModuleUrlRewrite
    {
     
        protected override string GetUrl(HttpContext context)
        {
            bool flag;
            string rewritedURL = AdvancedUrlRewriter.GetRewritedURL(context.Request.RawUrl, out flag);
            if (flag)
            {
                //add logic here
                return rewritedURL;
            }
            return string.Empty;
        }
    }


    Regards,
    Radoslav Georgiev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  14. Joe
    Joe avatar
    16 posts
    Registered:
    10 May 2010
    05 Jan 2011
    Link to this post
    Hi Radoslav,

    That's great it worked.

    I'm not sure if you can help me with this final query, I apologise for all these questions but we're currently trying to become as as search engine friendly as possible.

    I tried the following code, and it worked great
    public class CustomCmsHttpModuleUrlRewrite : CmsHttpModuleUrlRewrite
    {
     
        protected override string GetUrl(HttpContext context)
        {
            bool flag = false;
            string rewritedURL = AdvancedUrlRewriter.GetRewritedURL(context.Request.RawUrl, flag);
            string lang = PageUtilities.GetLanguageCookie;
     
     
            if (flag) {
                if (rewritedURL.Contains("?sflang=")) {
                    rewritedURL = rewritedURL.Replace("?sflang=" + lang, "");
                } else if (rewritedURL.Contains("&sflang=")) {
                    rewritedURL = rewritedURL.Replace("&sflang=" + lang, "");
                }
     
                // If the base link doesn't have a question "?", then add it now.
                if ((!rewritedURL.Contains("?"))) {
                    rewritedURL += "?";
                } else if ((!rewritedURL.EndsWith("?")) && (!rewritedURL.EndsWith("&")))             {
                    // If it has a question mark, and the last character is not a question mark, then add an &
                    rewritedURL += "&";
                }
     
                if (!rewritedURL.Contains("?sflang=")) {
                    rewritedURL += "sflang=" + lang;
                }
                return rewritedURL;
            }
            return string.Empty;
        }
    }

    The problem that I didn't foresee is that the sflang query string will be added to any other page which is not rewritten. Is there absolutely no way of removing the language for URLs and persisting with the cookie we have stored?

    Thanks
  15. Joe
    Joe avatar
    16 posts
    Registered:
    10 May 2010
    06 Jan 2011
    Link to this post
    I changed the code to the following to use persistence mode to PathPrefix:
    public class CustomCmsHttpModuleUrlRewrite : CmsHttpModuleUrlRewrite
    {
     
        protected override string GetUrl(HttpContext context)
        {
            bool flag = false;
            string rewritedURL = AdvancedUrlRewriter.GetRewritedURL(context.Request.RawUrl, flag);
            string lang = PageUtilities.GetLanguageCookie;
     
            string appPath = context.Request.ApplicationPath;
     
            if (flag) {
                if (!(rewritedURL.Contains("en-GB") || rewritedURL.Contains("it-IT"))) {
                    rewritedURL = rewritedURL.Replace(appPath, appPath + "/" + lang + "/");
                }
     
                return rewritedURL;
            }
            return string.Empty;
        }
    }

    So if a user does not enter the culture code to a rewritten URL it will not need redirecting as the actual URL will still have the culture code.

    Thanks Radoslav.
  16. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    06 Jan 2011
    Link to this post
    Hello Massimiliano Rizzuto,

    Since the code for handling localization without query string or path prefix is not implemented in Sitefinity 4.0 you would have to put some code that can handle the setting of culture in this method even if the url is not rewritten.

    Kind regards,
    Radoslav Georgiev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Register for webinar
16 posts, 0 answered