title: mod_xsendfile won't work with CGI and mod_rewrite
body: <p>I am trying to use the <a href="https://tn123.org/mod_xsendfile/" rel="nofollow noreferrer">apache module xsendfile</a> to get a better performance during file streaming.
  The problem is, that it is <strong>just working if I DO NOT use PHP as CGI Version AND if I DO NOT USE rewrite rules</strong> for my urls.</p>
  
  <h2>Problem 1: mod_rewrite</h2>
  
  <p>Calling this one in the Browser will work:</p>
  
  <pre><code>http://subdomain.domain.de/image.php
  </code></pre>
  
  <p>This one will give me a 404:</p>
  
  <pre><code>http://subdomain.domain.de/rewrittenImageUrl
  </code></pre>
  
  <p>The rewrite rules are working right. The 404 error is triggered by the xsendfile module.
  If I add a "R" to the rule in the htaccess (<a href="https://stackoverflow.com/questions/8866549/confilict-between-rewriteengine-and-xsendfile">like suggested in this question</a>) it will work again, because I am redirected to the first address given above. But redirecting is not what I want.</p>
  
  <p>I also watched out <a href="https://stackoverflow.com/questions/12809526/xsendfile-wont-work-with-mod-rewrite">this post</a> about symlinks, but I think this could not be a solution for my post as long as I use absolute paths generated by using getenv('document_root')? This pathes shouldn't use any symbolic links, do they? Or am I missing something at this point?</p>
  
  <h2>Problem 2: CGI</h2>
  
  <p>If I switch the PHP mode to the cgi version I will get a 0 byte file. This seems to be the same behaviour like the server would react without any installation of xsendfile.</p>
  
  <hr>
  
  <p>I have already updated the module to the latest version.
  Also tested absolute and relative links without any success.
  In addition to that <a href="https://stackoverflow.com/questions/8707944/xsendfile-not-working-php-apache-cpanel">Deactivating the output compression</a> didn't work.</p>
  
  <p>To complete the given information here is the PHP code I am using:</p>
  
  <pre><code>ini_set('zlib.output_compression', 'Off');
  $realImagePath = getenv('document_root')."fixedRelativeImagePathNoParamsNeeded.jpg";
  
  $imageInfos = @getimagesize($realImagePath);
  header('Content-Type: '.$imageInfos['mime']);   
  header("X-Sendfile: $realImagePath");
  exit();
  </code></pre>
  
  <p>Anyone has a clue?</p>
  
  <h2>Update (2014-01-06)</h2>
  
  <p><strong>The second problem is solved</strong>:
  I don't know why but it's working after turning on xsendfile in the apache config instead of using the htaccess file. (I will add an answer below as soons as the other problem is solved, too.)</p>
  
  <p>In addition to the first one:
  First I did not add any options in the httpd.conf as it should be working with the standard configuration. Anyway I now asked my provider to add the absolute project path to the whitelist of XSendFilePath as a global setting. <strong>This temporarily solved the 1. Problem with mod_rewrite.</strong> But this just seems to be not a real solution for my situation, because I am running many different projects on the server, each with a separated download path. So I would need to ask my provider to add a new path to the config each time I am starting a new project.
  I still just can't use x-sendfile with mod_rewrotite although I should have access to the document root without any extra settings.</p>