title: Nginx no-www to www and www to no-www
body: <p>I am using <a href="http://www.howtoforge.com/running-phpmyadmin-on-nginx-lemp-on-debian-squeeze-ubuntu-11.04" rel="noreferrer">nginx on Rackspace cloud following a tutorial</a> and having searched the net and so far can't get this sorted.</p>
  <p>I want <code>www.mysite.example</code> to go to <code>mysite.example</code> as normal in .htaccess for SEO and other reasons.</p>
  <p>My <strong>/etc/nginx/sites-available/www.example.com.vhost</strong> config:</p>
  <pre><code>server {
         listen 80;
         server_name www.example.com example.com;
         root /var/www/www.example.com/web;
  
         if ($http_host != &quot;www.example.com&quot;) {
                   rewrite ^ http://example.com$request_uri permanent;
         }
  </code></pre>
  <p>I have also tried</p>
  <pre><code>server {
         listen 80;
         server_name example.com;
         root /var/www/www.example.com/web;
  
         if ($http_host != &quot;www.example.com&quot;) {
                   rewrite ^ http://example.com$request_uri permanent;
         }
  </code></pre>
  <p>I also tried. Both the second attempts give redirect loop errors.</p>
  <pre><code>if ($host = 'www.example.com' ) {
  rewrite ^ http://example.com$uri permanent;
  }
  </code></pre>
  <p>My DNS is setup as standard:</p>
  <pre><code>site.example 192.192.6.8 A type at 300 seconds
  www.site.example 192.192.6.8 A type at 300 seconds
  </code></pre>
  <p>(example IPs and folders have been used for examples and to help people in future). I use Ubuntu 11.</p>