title: Avoid infinite loop with 301 redirects
body: <p>I'm using <code>.htaccess</code> to redirect </p>
  
  <pre><code>http://www.example.com/foo/
  </code></pre>
  
  <p>to</p>
  
  <pre><code>http://www.example.com/foo/bar
  </code></pre>
  
  <p>This is my code:</p>
  
  <pre><code>redirect 301 /foo/ http://www.example.com/foo/bar
  </code></pre>
  
  <p>However this produces a feedback loop, something like </p>
  
  <p><code>http://www.example.com/foo/barbarbarbarbarbar</code> etc.</p>
  
  <p>I've tried placing delimiters around it:</p>
  
  <pre><code>redirect 301 ^/foo/$ http://www.example.com/foo/bar
  </code></pre>
  
  <p>but then the redirect simply doesn't take place. I'm probably missing some very simple point of syntax. Any ideas? Thanks.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here's my (almost) full <code>.htaccess</code> file:</p>
  
  <pre><code>RewriteEngine On
  RewriteBase /
  
  # Canonical is www version
  RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
  RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
  
  #redirect =&gt; http unless special page
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{HTTPS} on
  RewriteCond %{REQUEST_URI} !^/(javascripts|images|library|stylesheets)
  RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
  
  #redirect =&gt; https for special pages
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{HTTPS} off
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
  
  # send to router
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php [L]
  </code></pre>