Blog posts by Kristoffer Petersson2009-10-30T14:23:02.0000000Z/blogs/Kristoffer-Petersson/Optimizely WorldFriendly Url and mobile phones/blogs/Kristoffer-Petersson/Dates/2009/10/Friendly-Url-and-mobile-phones/2009-10-30T14:23:02.0000000Z<p>The EPiServerFriendlyUrlRewriteProvider in EPiServer CMS 5.2 SP2 can cause navigational problems for visitors using mobile phones. I have noticed that the browser in some Ericsson mobile phones does not handle friendly URL’s very well. </p> <p>Recently this became a problem for a site I have been working on. Menus and all navigation worked well in standard web browsers, however navigating the site from my Ericsson mobile phone was simply not possible. EPiServer lost track of the current page and all menus were loaded from the root page.</p> <p>One obvious solution was to use the EPiServerNullUrlRewriteProvider and go back to using the old style URL’s (somefolder/somepage.aspx?id=12345). This was not possible for the site I was working on. </p> <p>My solution was to stop the rewriting of URL’s pointing to pages design for mobile phones and handle the loading of these pages in code. <br />The first step is to stop rewriting of specific URL’s, in my case all paths containing “/mobile”. This can bee done in Global.asax.</p> <p>In Application_Start add the following:</p> <code>EPiServer.Web.UrlRewriteModule.HttpRewriteInit += UrlRewriteModule_HttpRewriteInit; </code> <p>Then add the following to Global.asax</p> <p><code>private void UrlRewriteModule_HttpRewriteInit(object sender, UrlRewriteEventArgs e) { UrlRewriteModuleBase module = sender as UrlRewriteModuleBase; if (module != null) { module.HtmlAddingRewriteToExternalFilter += module_HtmlAddingRewriteToExternalFilter; } } private void module_HtmlAddingRewriteToExternalFilter(object sender, UrlRewriteEventArgs e) { string path = e.Url.Path.ToLowerInvariant(); e.Cancel = path.Contains("/mobil"); } </code></p> <p>Now rewriting will be cancelled for URL’s containing “/mobile”. However this alone did not solve the problem. EPiServer still couldn’t load the correct page. The second step is to handle loading of pages.</p> <p>I retrieve the PageData object for the desired page using the ID-parameter from the raw URL.</p> <code>PageData pd = null; System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(url, "(\\?id=)[0-9]*"); if (!String.IsNullOrEmpty(m.Value) && int.TryParse(m.Value.Remove(0, 4), out id)) { pd = DataFactory.Instance.GetPage(new PageReference(id)); } return pd; </code> <p>With use of the PageData object I can load the correct page, access properties and initiate menus. Problem solved for now. I am looking for a better solution, do you have one?</p>