title: site redirection to mobile site using asp.net and Application_AcquireRequestState
body: <p>I have website in asp.net and have its mobile version as well. I want that if user types this in mobile:</p>
  
  <p><a href="http://example.com/ViewVacancy.aspx?ID=8674" rel="nofollow">http://example.com/ViewVacancy.aspx?ID=8674</a></p>
  
  <p>then he is directed to:</p>
  
  <p><a href="http://m.example.com/vacancy.aspx?name=8674" rel="nofollow">http://m.example.com/vacancy.aspx?name=8674</a></p>
  
  <p>I have written following code:</p>
  
  <pre><code>protected void Application_AcquireRequestState(object sender, EventArgs e)
      {
  
  
          HttpContext context = HttpContext.Current;
  
  
          if (context.Request.Browser.IsMobileDevice)
          {
  
              string uri = context.Request.Url.Authority.ToString(); 
  
              if (context.Request.Url.PathAndQuery.ToLower().Contains("viewvacancy"))
              {
                  string query = context.Request.Url.Query; 
                  query = query.Replace("ID", "Name");
                  if (uri.ToString().StartsWith("http"))
                  {
                      Uri nmobileURi = new Uri(context.Request.Url.Scheme + "://" + "m.example.com/vacancy.aspx" + query);
                      Response.Redirect(nmobileURi.ToString());
                  }               
              }
              else
              {
                  Response.Redirect("http://m.example.com");
              }
          }
          if (context.Request.ServerVariables["HTTP_USER_AGENT"] != null)
          {
  
              var userAgent = HttpContext.Current.Request.UserAgent.ToLower();
              if (userAgent.Contains("iphone") || userAgent.Contains("ipod")) 
              {
                  Response.Redirect("http://example.com/downloadapp.htm");
                  // iPhone
              }
  
              else if (userAgent.Contains("android"))
              {
                  Response.Redirect("http://example.com/downloadgoogleplay.htm");
              }
  
              //Create a list of all mobile types
              string[] mobiles =
              new[]
              { "midp", "j2me", "avant", "docomo",
              "novarra", "palmos", "palmsource",
              "240×320", "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
              foreach (string s in mobiles)
              {
                  if (context.Request.ServerVariables["HTTP_USER_AGENT"].
                  ToLower().Contains(s.ToLower()))
                  {                   
                      string uri = context.Request.Url.Authority.ToString(); 
  
                      if (context.Request.Url.PathAndQuery.ToLower().Contains("viewvacancy"))
                      {
                          string query = context.Request.Url.Query; 
                          query = query.Replace("ID", "Name");
                          if (uri.ToString().StartsWith("http"))
                          {
                              Uri nmobileURi = new Uri(context.Request.Url.Scheme + "://" + "m.example.com/vacancy.aspx" + query);
                              Response.Redirect(nmobileURi.ToString());
                          }
                      }
                      else
                      {
                          Response.Redirect("http://m.example.com");
                      }
                  }
              }
          }
      }
  </code></pre>
  
  <p>It is working when user types</p>
  
  <p><a href="http://example.com" rel="nofollow">http://example.com</a></p>
  
  <p>so user is redirected to:</p>
  
  <p><a href="http://m.example.com" rel="nofollow">http://m.example.com</a></p>
  
  <p>but when user types:</p>
  
  <p><a href="http://example.com/viewvacancy.aspx?id=8674" rel="nofollow">http://example.com/viewvacancy.aspx?id=8674</a></p>
  
  <p>It is not redirected to:</p>
  
  <p><a href="http://m.example.com/vacancy.aspx?name=8674" rel="nofollow">http://m.example.com/vacancy.aspx?name=8674</a></p>
  
  <p>Please suggest solution to it.</p>