title: Apache: RewriteRule leads to segfault
body: <p>I use a RewriteRule to replace all spaces (aka %20) in my URLs by underscores:</p>
  
  <pre><code>RewriteRule (.*)[\ ](.*) $1_$2 [N]
  </code></pre>
  
  <p>The [N] flag starts the rewrite process again, until no space is left (hopefully). Now, all is well, when there is a file waiting at the other side, i.e. request:</p>
  
  <pre><code>/This is an example.html
  </code></pre>
  
  <p>and file:</p>
  
  <pre><code>This_is_an_example.html
  </code></pre>
  
  <p>But when there is no matching file, Apache exits with a 500 Internal Error. The error log states a segfault, and the rewrite log shows, that the rewrite engine goes mad trying to redirect to</p>
  
  <pre><code>/This is an_example.html/This is an_example.html/This is an_example.html/...
  </code></pre>
  
  <p>and so on until the segfault (note, that the <em>last</em> space was converted, but none else).</p>
  
  <p>Has anyone an idea, why the RewriteRule fails so miserably, while working for existing files?</p>
  
  <p><strong>Update:</strong> The segfault only occurs, if there is an additional "/" in the requested URI, like</p>
  
  <pre><code>/virtual-directory/This is an example.html
  </code></pre>
  
  <p><strong>Update 2:</strong> There is <em>no</em> RewriteCond in this statement. In my opinion it should work without. The question is, why doesn't it. Htaccess:</p>
  
  <pre><code>RewriteEngine On
  RewriteBase /test/
  
  RewriteRule (.*)[\ ](.*) $1_$2 [N]
  </code></pre>
  
  <p><strong>Update 3:</strong> The .htaccess file in question:</p>
  
  <pre><code>AddType application/xhtml+xml .xhtml
  AddEncoding x-gzip .gz
  
  ExpiresActive On
  ExpiresByType application/xhtml+xml "access plus 1 year"
  
  ErrorDocument 404 /test/index.php?error=404
  
  RewriteEngine On
  
  RewriteBase /test/
  
  # replace spaces with underscores
  RewriteCond %{REQUEST_FILENAME} .*[\ ].*
  RewriteRule (.*)[\ ](.*) $1_$2 [N]
  
  # the index.php
  RewriteRule ^index.php - [L]
  
  # language tag
  RewriteRule ^([a-z]{2})$ index.php?lang=$1&amp;url= [L,QSA]
  RewriteRule ^$ en [R=301,L]
  
  # add .xhtml
  RewriteCond %{REQUEST_FILENAME}.xhtml -s [OR]
  RewriteCond %{REQUEST_FILENAME}.xhtml.gz -s
  RewriteRule ^(.+) $1.xhtml
  
  # add .gz
  RewriteCond %{HTTP:Accept-Encoding} .*gzip.*
  RewriteCond %{REQUEST_FILENAME}.gz -s
  RewriteRule ^(.+) $1.gz
  
  # send correct mime type
  #RewriteCond %{REQUEST_FILENAME} -s
  RewriteRule \.xhtml\.gz$  - [L,T=application/xhtml+xml]
  </code></pre>
  
  <p>The rewriteLog statements are too large to paste. A single request fills the file with 13MB of data. Here is the digest:</p>
  
  <pre><code>(IP) - - [date] [host] (3) [perdir /htdocs/test/] add path info postfix: /htdocs/test/de -&gt; /htdocs/test/de/This is a test
  (IP) - - [date] [host] (3) [perdir /htdocs/test/] strip per-dir prefix: /htdocs/test/de/This is a test -&gt; de/This is a test
  (IP) - - [date] [host] (3) [perdir /htdocs/test/] applying pattern '(.*)[\ ](.*)' to uri 'de/This is a test'
  (IP) - - [date] [host] (2) [perdir /htdocs/test/] rewrite 'de/This is a test' -&gt; 'de/This is a_test'
  (IP) - - [date] [host] (3) [perdir /htdocs/test/] add per-dir prefix: de/This is a_test -&gt; /htdocs/test/de/This is a_test
  (IP) - - [date] [host] (3) [perdir /htdocs/test/] add path info postfix: /htdocs/test/de/This is a_test -&gt; /htdocs/test/de/This is a_test/This is a test
  (IP) - - [date] [host] (3) [perdir /htdocs/test/] strip per-dir prefix: /htdocs/test/de/This is a_test/This is a test -&gt; de/This is a_test/This is a test
  (IP) - - [date] [host] (3) [perdir /htdocs/test/] applying pattern '(.*)[\ ](.*)' to uri 'de/This is a_test/This is a test'
  (IP) - - [date] [host] (2) [perdir /htdocs/test/] rewrite 'de/This is a_test/This is a test' -&gt; 'de/This is a_test/This is a_test'
  (IP) - - [date] [host] (3) [perdir /htdocs/test/] add per-dir prefix: de/This is a_test/This is a_test -&gt; /htdocs/test/de/This is a_test/This is a_test
  (IP) - - [date] [host] (3) [perdir /htdocs/test/] add path info postfix: /htdocs/test/de/This is a_test/This is a_test -&gt; /htdocs/test/de/This is a_test/This is a_test/This is a test
  (IP) - - [date] [host] (3) [perdir /htdocs/test/] strip per-dir prefix: /htdocs/test/de/This is a_test/This is a_test/This is a test -&gt; de/This is a_test/This is a_test/This is a test
  (IP) - - [date] [host] (3) [perdir /htdocs/test/] applying pattern '(.*)[\ ](.*)' to uri 'de/This is a_test/This is a_test/This is a test'
  (IP) - - [date] [host] (2) [perdir /htdocs/test/] rewrite 'de/This is a_test/This is a_test/This is a test' -&gt; 'de/This is a_test/This is a_test/This is a_test'
  (IP) - - [date] [host] (3) [perdir /htdocs/test/] add per-dir prefix: de/This is a_test/This is a_test/This is a_test -&gt; /htdocs/test/de/This is a_test/This is a_test/This is a_test
  ... and so on ad infinitum
  </code></pre>
  
  <p>The bad guy is line 6: "add path info postfix".</p>