I use a RewriteRule to replace all spaces (aka %20) in my URLs by underscores:
RewriteRule (.*)[\ ](.*) $1_$2 [N]
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:
/This is an example.html
and file:
This_is_an_example.html
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
/This is an_example.html/This is an_example.html/This is an_example.html/...
and so on until the segfault (note, that the last space was converted, but none else).
Has anyone an idea, why the RewriteRule fails so miserably, while working for existing files?
Update: The segfault only occurs, if there is an additional "/" in the requested URI, like
/virtual-directory/This is an example.html
Update 2: There is no RewriteCond in this statement. In my opinion it should work without. The question is, why doesn't it. Htaccess:
RewriteEngine On
RewriteBase /test/
RewriteRule (.*)[\ ](.*) $1_$2 [N]
Update 3: The .htaccess file in question:
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&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]
The rewriteLog statements are too large to paste. A single request fills the file with 13MB of data. Here is the digest:
(IP) - - [date] [host] (3) [perdir /htdocs/test/] add path info postfix: /htdocs/test/de -> /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 -> 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' -> 'de/This is a_test'
(IP) - - [date] [host] (3) [perdir /htdocs/test/] add per-dir prefix: de/This is a_test -> /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 -> /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 -> 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' -> '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 -> /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 -> /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 -> 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' -> '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 -> /htdocs/test/de/This is a_test/This is a_test/This is a_test
... and so on ad infinitum
The bad guy is line 6: "add path info postfix".