title: htaccess ErrorDocument not always redirecting
body: <p>I have the basic code for redirecting to a 404 custom page if a 404 not found error occurs but this does not always work as expected. I also have a rule to rewrite URLs where <code>page/foo/bar</code> is redirected to <code>page.php?var1=foo&amp;var2=bar</code></p>
  
  <p>The rule for this is the following:</p>
  
  <pre><code>&lt;IfModule mod_rewrite.c&gt;
      RewriteCond %{REQUEST_FILENAME} -f [OR]
      RewriteCond %{REQUEST_FILENAME} -d
      RewriteRule .* - [L]
      RewriteRule ^page/(.*)/(.*)$ page.php?var1=$1&amp;var2=$2 [L]
  &lt;/IfModule&gt;
  </code></pre>
  
  <p>So, this works very well and I can retrieve the two variables. However, if I write in the URL bar <code>page/a</code> a error is thrown with the message <code>500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.</code> The message says the error is because of <code>ErrorDocument</code> rule. If I type a non-existent URL the <code>ErrorDocument</code> works, but in this particular case it does not. </p>
  
  <p>I also want to mention that if instead of writing <code>page/a</code> I write <code>page/a/</code>, the error disappears. So the error is thrown only when I pass <strong>one</strong> parameter only in the URL and without a <code>/</code>.</p>
  
  <p>For any other existing page, <code>about</code>, <code>contacts</code> etc.., I can easily put or remove the <code>/</code> and <code>.htaccess</code> automatically redirects me to the correct page, without the <code>/</code></p>
  
  <p>This is the code that modifies the URL removing the extension and the <code>/</code></p>
  
  <pre><code># ------------------------------------------------------------------------------
  # | Remove the slash                                                           |
  # ------------------------------------------------------------------------------
  &lt;IfModule mod_rewrite.c&gt;
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^([^/]+)/$ $1 [R=301,L]
  &lt;/IfModule&gt;
  
  # ------------------------------------------------------------------------------
  # | Redirect to extensionless url                                              |
  # ------------------------------------------------------------------------------
  &lt;IfModule mod_rewrite.c&gt;
      RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
      RewriteRule ^(.+)\.php$ $1 [R=301,L]
  &lt;/IfModule&gt;
  </code></pre>
  
  <p>And here is the code for ErrorDocument:</p>
  
  <pre><code>ErrorDocument 404 /boilerplate/template/404.php
  </code></pre>
  
  <p>Finally, the message in Apache error log:</p>
  
  <pre><code>[core:error] [pid 4400:tid 1672] [client ::1:65310] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.  
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>This is the entire part about rewriting</p>
  
  <pre><code># ##############################################################################
  # # URL REWRITES                                                               #
  # ##############################################################################
  # ------------------------------------------------------------------------------
  # | Rewrite engine                                                             |
  # ------------------------------------------------------------------------------
  
  
  &lt;IfModule mod_rewrite.c&gt;
      Options +FollowSymlinks
    # Options +SymLinksIfOwnerMatch
      RewriteEngine On
   RewriteBase /boilerplate
  &lt;/IfModule&gt;
  
  
  # ------------------------------------------------------------------------------
  # | Remove the slash                                                           |
  # ------------------------------------------------------------------------------
  &lt;IfModule mod_rewrite.c&gt;
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^([^/]+)/$ $1 [R=301,L]
  &lt;/IfModule&gt;
  
  # ------------------------------------------------------------------------------
  # | Redirect to extensionless url                                              |
  # ------------------------------------------------------------------------------
  &lt;IfModule mod_rewrite.c&gt;
      RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
      RewriteRule ^(.+)\.php$ $1 [R=301,L]
  &lt;/IfModule&gt;
  
  # ------------------------------------------------------------------------------
  # | Pretty urls                                                                |
  # ------------------------------------------------------------------------------
  &lt;IfModule mod_rewrite.c&gt;
      RewriteCond %{REQUEST_FILENAME} -f [OR]
      RewriteCond %{REQUEST_FILENAME} -d
      RewriteRule .* - [L]
      RewriteRule ^about/([^/]+)(?:/([^/]*))?/?$ about.php?var1=$1&amp;var2=$2 [L,NC,QSA]
  &lt;/IfModule&gt;
  
  &lt;IfModule mod_rewrite.c&gt;
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME}\.php -f
      RewriteRule ^(.*)$ $1.php [L]
  &lt;/IfModule&gt;
  </code></pre>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>So, I deleted everything from <code>.htaccess</code> except these few rules. Nothing has changed. I cleared the cookies, emptied the cache and cleared all browser history. Also, I tried it with other browsers and the same thing happens. Just to be clear, I don't have another <code>.htaccess</code> file and the original one contains other snippets from HTML5 Boilerplate, such as caching, compressing and other non re-write related. Now I stripped the file down to these few lines. </p>
  
  <pre><code>    Options +FollowSymlinks
      Options +SymLinksIfOwnerMatch
      RewriteEngine On
      RewriteBase /boilerplate
  
  # ------------------------------------------------------------------------------
  # | Redirect to extensionless url                                              |
  # ------------------------------------------------------------------------------
  
      RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
      RewriteRule ^(.+)\.php$ $1 [R=301,L]
  
  
  # ------------------------------------------------------------------------------
  # | Pretty urls                                                                |
  # ------------------------------------------------------------------------------
  
      RewriteCond %{REQUEST_FILENAME} -f [OR]
      RewriteCond %{REQUEST_FILENAME} -d
      RewriteRule .* - [L]
      RewriteRule ^page/([^/]+)(?:/([^/]*))?/?$ page.php?var1=$1&amp;var2=$2 [L,NC,QSA]
  
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME}\.php -f
      RewriteRule ^(.*)$ $1.php [L]
  </code></pre>