Sitecore Mobile Site as subdomain m (m.thedomain.com)

Create a layout MobileLayout.aspx for your mobile site and paste the code in OnInit


       string _absoluteUri = String.Format("{0}://{1}{2}", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Host, HttpContext.Current.Request.RawUrl);
        string _host = HttpContext.Current.Request.Url.Host;
        private string _isMobileSet;


  protected override void OnInit(EventArgs e)
        {

            if (Util.IsMobileBrowser(HttpContext.Current))
            {
                if (HttpContext.Current.Session["IsMobileSet"] != null)
                {
                    string isMobileSetSession = HttpContext.Current.Session["IsMobileSet"].ToString();

                    switch (isMobileSetSession)
                    {
                        case "false":
                            {
                                QueryString qs = new QueryString(_absoluteUri);

                                if (qs.HasParameter("IsMobileSet"))
                                {
                                    _isMobileSet = qs.GetParameter("IsMobileSet");

                                    switch (_isMobileSet)
                                    {
                                        case "false":
                                            {
                                                _host = _host.Replace("m.", "");
                                                _absoluteUri = _absoluteUri.Replace(HttpContext.Current.Request.Url.Host, _host);
                                                qs = new QueryString(_absoluteUri);
                                                qs.SetParameter("IsMobileSet", "false");
                                                HttpContext.Current.Response.Redirect(qs.All);
                                            }
                                            break;

                                        case "true":
                                            {
                                                HttpContext.Current.Session["IsMobileSet"] = "true";
                                            }
                                            break;
                                        default:
                                            {
                                                HttpContext.Current.Session["IsMobileSet"] = "true";
                                            }
                                            break;
                                    }

                                }
                                else
                                {
                                    _host = _host.Replace("m.", "");
                                    _absoluteUri = _absoluteUri.Replace(HttpContext.Current.Request.Url.Host, _host);
                                    qs = new QueryString(_absoluteUri);
                                    qs.SetParameter("IsMobileSet", "false");
                                    HttpContext.Current.Response.Redirect(qs.All);
                                }
                            }
                            break;

                        case "true":
                            {
                                QueryString qs = new QueryString(_absoluteUri);

                                if (qs.HasParameter("IsMobileSet"))
                                {
                                    _isMobileSet = qs.GetParameter("IsMobileSet");

                                    switch (_isMobileSet)
                                    {
                                        case "false":
                                            {

                                                _host = _host.Replace("m.", "");
                                                _absoluteUri = _absoluteUri.Replace(HttpContext.Current.Request.Url.Host, _host);
                                                qs = new QueryString(_absoluteUri);
                                                qs.SetParameter("IsMobileSet", "false");
                                                HttpContext.Current.Response.Redirect(qs.All);
                                            }
                                            break;
                                    }
                                }
                            }
                            break;
                    }
                }
                else
                {
                    QueryString qs = new QueryString(_absoluteUri);

                    if (qs.HasParameter("IsMobileSet"))
                    {
                        _isMobileSet = qs.GetParameter("IsMobileSet");

                        switch (_isMobileSet)
                        {
                            case "false":
                                {
                                    HttpContext.Current.Session["IsMobileSet"] = "false";
                                    _host = _host.Replace("m.", "");
                                    _absoluteUri = _absoluteUri.Replace(HttpContext.Current.Request.Url.Host, _host);
                                    qs = new QueryString(_absoluteUri);
                                    HttpContext.Current.Response.Redirect(qs.All);
                                }
                                break;
                            default:
                                HttpContext.Current.Session["IsMobileSet"] = "true";
                                break;
                        }
                    }
                    else
                        HttpContext.Current.Session["IsMobileSet"] = "true";
                }
            }
            else
            {
                _host = _host.Replace("m.", "");
                _absoluteUri = _absoluteUri.Replace(HttpContext.Current.Request.Url.Host, _host);
                QueryString qs = new QueryString(_absoluteUri);
                HttpContext.Current.Session["IsMobileSet"] = "false";
                qs.SetParameter("IsMobileSet", "false");
                HttpContext.Current.Response.Redirect(qs.All);

            }


            base.OnInit(e);
        }

On your Desktop Layout use the following code:


        string _absoluteUri = String.Format("{0}://{1}{2}", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Host, HttpContext.Current.Request.RawUrl);
        string _host = HttpContext.Current.Request.Url.Host;
        private string _isMobileSet;
        Item _item = Sitecore.Context.Item;

        protected override void OnInit(EventArgs e)
        {
            if (Util.IsMobileBrowser(HttpContext.Current))
            {
                bool hasMobileDevie = false;

                LayoutDefinition layout = LayoutDefinition.Parse(_item[FieldIDs.LayoutField]);

                if (layout.Devices.Count > 0)
                {
                    if (layout.Devices.Cast<DeviceDefinition>().Any(device => device.ID.Contains(Constants.MobileDeviceId)))
                        hasMobileDevie = true;
                    else
                    {

                        if (_item.Fields[FieldIDs.LayoutField].ContainsStandardValue && _item.Template.StandardValues != null)
                        {
                            _item = _item.Template.StandardValues;

                            layout = LayoutDefinition.Parse(_item[FieldIDs.LayoutField]);

                            if (layout.Devices.Count > 0)
                            {
                                if (layout.Devices.Cast<DeviceDefinition>().Any(device => device.ID.Contains(Constants.MobileDeviceId)))
                                    hasMobileDevie = true;

                            }
                        }

                    }

                }
                else
                {
                    if (_item.Template.StandardValues != null)
                    {
                        _item = _item.Template.StandardValues;

                        layout = LayoutDefinition.Parse(_item[FieldIDs.LayoutField]);

                        if (layout.Devices.Count > 0)
                        {
                            if (layout.Devices.Cast<DeviceDefinition>().Any(device => device.ID.Contains(Constants.MobileDeviceId)))
                                hasMobileDevie = true;

                        }
                    }
                }

                if (hasMobileDevie)
                {
                    if (HttpContext.Current.Session["IsMobileSet"] != null)
                    {
                        string isMobileSetSession = HttpContext.Current.Session["IsMobileSet"].ToString();

                        switch (isMobileSetSession)
                        {
                            case "true":
                                {
                                    QueryString qs = new QueryString(_absoluteUri);

                                    if (qs.HasParameter("IsMobileSet"))
                                    {
                                        _isMobileSet = qs.GetParameter("IsMobileSet");

                                        switch (_isMobileSet)
                                        {
                                            case "true":
                                                {
                                                    if (!_host.Contains("m."))
                                                        _host = String.Format("{0}{1}", "m.", _host);

                                                    _absoluteUri = _absoluteUri.Replace(HttpContext.Current.Request.Url.Host, _host);
                                                    qs = new QueryString(_absoluteUri);
                                                    qs.SetParameter("IsMobileSet", "true");
                                                    HttpContext.Current.Response.Redirect(qs.All);
                                                }
                                                break;

                                            case "false":
                                                {
                                                    HttpContext.Current.Session["IsMobileSet"] = "false";
                                                }
                                                break;

                                            default:
                                                {
                                                    if (!_host.Contains("m."))
                                                        _host = String.Format("{0}{1}", "m.", _host);

                                                    _absoluteUri = _absoluteUri.Replace(HttpContext.Current.Request.Url.Host, _host);
                                                    qs = new QueryString(_absoluteUri);
                                                    qs.SetParameter("IsMobileSet", "true");
                                                    HttpContext.Current.Response.Redirect(qs.All);
                                                }
                                                break;
                                        }
                                    }
                                    else
                                    {

                                        if (!_host.Contains("m."))
                                            _host = String.Format("{0}{1}", "m.", _host);

                                        _absoluteUri = _absoluteUri.Replace(HttpContext.Current.Request.Url.Host, _host);
                                        qs = new QueryString(_absoluteUri);
                                        qs.SetParameter("IsMobileSet", "true");
                                        HttpContext.Current.Response.Redirect(qs.All);
                                    }
                                }
                                break;

                            case "false":
                                {
                                    QueryString qs = new QueryString(_absoluteUri);

                                    if (qs.HasParameter("IsMobileSet"))
                                    {
                                        _isMobileSet = qs.GetParameter("IsMobileSet");

                                        switch (_isMobileSet)
                                        {
                                            case "true":
                                                {
                                                    if (!_host.Contains("m."))
                                                        _host = String.Format("{0}{1}", "m.", _host);

                                                    _absoluteUri = _absoluteUri.Replace(HttpContext.Current.Request.Url.Host, _host);
                                                    qs = new QueryString(_absoluteUri);
                                                    HttpContext.Current.Session["IsMobileSet"] = "true";
                                                    qs.SetParameter("IsMobileSet", "true");
                                                    HttpContext.Current.Response.Redirect(qs.All);
                                                }
                                                break;
                                        }
                                    }
                                }
                                break;
                        }
                    }
                    else
                    {
                        QueryString qs = new QueryString(_absoluteUri);

                        if (qs.HasParameter("IsMobileSet"))
                        {
                            _isMobileSet = qs.GetParameter("IsMobileSet");

                            switch (_isMobileSet)
                            {
                                case "false":
                                    {
                                        HttpContext.Current.Session["IsMobileSet"] = "false";
                                    }
                                    break;

                                case "true":
                                    {
                                        if (_host.Contains("www"))
                                        {
                                            _host = _host.Replace("www", "m");
                                            _absoluteUri = _absoluteUri.Replace(HttpContext.Current.Request.Url.Host, _host);
                                            qs = new QueryString(_absoluteUri);
                                            qs.SetParameter("IsMobileSet", "true");
                                            HttpContext.Current.Session["IsMobileSet"] = "true";
                                            HttpContext.Current.Response.Redirect(qs.All);
                                        }
                                        else
                                        {
                                            if (!_host.Contains("m."))
                                                _host = String.Format("{0}.{1}", "m", HttpContext.Current.Request.Url.Host);

                                            _absoluteUri = _absoluteUri.Replace(HttpContext.Current.Request.Url.Host, _host);
                                            qs = new QueryString(_absoluteUri);
                                            qs.SetParameter("IsMobileSet", "true");
                                            HttpContext.Current.Session["IsMobileSet"] = "true";
                                            HttpContext.Current.Response.Redirect(qs.All);
                                        }
                                    }
                                    break;

                                default:
                                    HttpContext.Current.Session["IsMobileSet"] = "false";
                                    break;
                            }
                        }
                        else
                        {
                            if (_host.Contains("www"))
                            {
                                _host = _host.Replace("www", "m");
                                _absoluteUri = _absoluteUri.Replace(HttpContext.Current.Request.Url.Host, _host);
                                qs = new QueryString(_absoluteUri);
                                qs.SetParameter("IsMobileSet", "true");
                                HttpContext.Current.Session["IsMobileSet"] = "true";
                                HttpContext.Current.Response.Redirect(qs.All);
                            }
                            else
                            {
                                if (!_host.Contains("m."))
                                    _host = String.Format("{0}.{1}", "m", HttpContext.Current.Request.Url.Host);

                                _absoluteUri = _absoluteUri.Replace(HttpContext.Current.Request.Url.Host, _host);
                                qs = new QueryString(_absoluteUri);
                                qs.SetParameter("IsMobileSet", "true");
                                HttpContext.Current.Session["IsMobileSet"] = "true";
                                HttpContext.Current.Response.Redirect(qs.All);
                            }
                        }

                    }

                }
            }
            else
            {
                if (_host.Contains("m."))
                {
                    _host = _host.Replace("m.", "");
                    _absoluteUri = _absoluteUri.Replace(HttpContext.Current.Request.Url.Host, _host);
                    QueryString qs = new QueryString(_absoluteUri);
                    HttpContext.Current.Session["IsMobileSet"] = "false";
                    qs.SetParameter("IsMobileSet", "false");
                    HttpContext.Current.Response.Redirect(qs.All);
                }
            }

            base.OnInit(e);
        }

a helper class to detect Mobile Device


    public static bool IsMobileBrowser(HttpContext context)
        {
            //FIRST TRY BUILT IN ASP.NT CHECK
            if (context.Request.Browser.IsMobileDevice)
            {
                return true;
            }
            //THEN TRY CHECKING FOR THE HTTP_X_WAP_PROFILE HEADER
            if (context.Request.ServerVariables["HTTP_X_WAP_PROFILE"] != null)
            {
                return true;
            }
            //THEN TRY CHECKING THAT HTTP_ACCEPT EXISTS AND CONTAINS WAP
            if (context.Request.ServerVariables["HTTP_ACCEPT"] != null &&
                context.Request.ServerVariables["HTTP_ACCEPT"].ToLower().Contains("wap"))
            {
                return true;
            }
            //AND FINALLY CHECK THE HTTP_USER_AGENT
            //HEADER VARIABLE FOR ANY ONE OF THE FOLLOWING
            if (context.Request.ServerVariables["HTTP_USER_AGENT"] != null)
            {
                //Create a list of all mobile types
                string[] mobiles =
                {
                    "midp", "j2me", "avant", "docomo",
                    "novarra", "palmos", "palmsource",
                    "240x320", "opwv", "chtml",
                    "pda", "windows ce", "mmp/",
                    "blackberry", "mib/", "symbian",
                    "wireless", "nokia", "hand", "mobi",
                    "phone", "cdm", "up.b", "audio",
                    "SIE-", "SEC-", "samsung", "HTC",
                    "mot-", "mitsu", "sagem", "sony"
                    , "alcatel", "lg", "eric", "vx",
                    "NEC", "philips", "mmm", "xx",
                    "panasonic", "sharp", "wap", "sch",
                    "rover", "pocket", "benq", "java",
                    "pt", "pg", "vox", "amoi",
                    "bird", "compal", "kg", "voda",
                    "sany", "kdd", "dbt", "sendo",
                    "sgh", "gradi", "jb", "dddi",
                    "moto", "iphone"
                };


                //Loop through each item in the list created above
                //and check if the header contains that text
                if (mobiles.Any(s => context.Request.ServerVariables["HTTP_USER_AGENT"].ToLower().Contains(s.ToLower())))
                    return true;
                
            }

            return false;
        }
    }

Setup your new site in web.config



      <event name="publish:end">
        <handler type="Sitecore.Publishing.HtmlCacheClearer, Sitecore.Kernel" method="ClearCache">
          <sites hint="list">
            <site>website</site>
            <site>mobile_website</site>
          </sites>
        </handler>
      </event>
      <event name="publish:end:remote">
        <handler type="Sitecore.Publishing.HtmlCacheClearer, Sitecore.Kernel" method="ClearCache">
          <sites hint="list">
            <site>website</site>
            <site>mobile_website</site>
          </sites>
        </handler>
      </event>


      <site name="mobile_website" hostName="m.*.com" defaultDevice="Mobile" urlExtension="" virtualFolder="/" physicalFolder="/" rootPath="/sitecore/content" startItem="/home" database="web" domain="extranet" allowDebug="true" cacheHtml="true" htmlCacheSize="100MB" registryCacheSize="0" viewStateCacheSize="0" xslCacheSize="5MB" filteredItemsCacheSize="2MB" enablePreview="true" enableWebEdit="true" enableDebugger="true" disableClientData="false" />
      <site name="website" urlExtension="" defaultDevice="Default" virtualFolder="/" physicalFolder="/" rootPath="/sitecore/content" startItem="/home" database="web" domain="extranet" allowDebug="true" cacheHtml="true" htmlCacheSize="100MB" registryCacheSize="0" viewStateCacheSize="0" xslCacheSize="5MB" filteredItemsCacheSize="2MB" enablePreview="true" enableWebEdit="true" enableDebugger="true" disableClientData="false" />
  


    <!-- CACHE SIZES -->
    <cacheSizes>
      <sites>
        <website>
          <html>100MB</html>
          <registry>0</registry>
          <viewState>0</viewState>
          <xsl>50MB</xsl>
        </website>
        <mobile_website>
          <html>100MB</html>
          <registry>0</registry>
          <viewState>0</viewState>
          <xsl>50MB</xsl>
        </mobile_website>
      </sites>
    </cacheSizes>


Advertisements

2 thoughts on “Sitecore Mobile Site as subdomain m (m.thedomain.com)

  1. This is great. I was just about to start a mobile site and this is perfect for what I need. thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s