title: Nginx User Agent Rewrite
body: <p>Aim: I would like to rewrite based on the below conditions:</p>
  
  <ul>
  <li>User agent is either Iphone or Blackberry</li>
  <li>There is not a "redirect=false" in the uri.</li>
  </ul>
  
  <p>I have the below code which I thought would work, but doesn't. It just redirects all the time if the user agent is set, but ignores the <code>$uri</code> condition.</p>
  
  <pre><code>location / {
    if ( $uri  ~* "redirect=false" ) {
      set $var_redirect F;
    }
  
    if ( $http_user_agent ~* "iPhone|BlackBerry" ) {
      set $var_redirect "${var_redirect}M";
    }
  
    if ( $var_redirect = M ) {
      rewrite ^ http://m.domain.com redirect;
    }
  }
  </code></pre>
  
  <p>The other option is to replace the redirect false condition with a only if location is / but not a sub folder</p>