title: How to redirect Apache "Index of ..." page?
body: <p>I am trying to achieve authenticated file listing in Apache using PHP via Apache's <a href="http://httpd.apache.org/docs/2.2/mod/mod_autoindex.html" rel="nofollow">autoindex module</a>.</p>
  
  <p>The way I imagined it was to have Apache run a PHP script as a <a href="http://httpd.apache.org/docs/2.2/mod/mod_autoindex.html#headername" rel="nofollow">header file</a>. I've managed to get Apache run PHP correctly for the header file and it detects login cookies fine, too. But it seems that Apache runs the header file as a separate request which means if I try to send a redirection header from PHP it is not run.</p>
  
  <p>My (simplified) Apache config:</p>
  
  <pre><code>DocumentRoot "/path/to/files_root"
  Alias /~extra "/path/to/extra-data"
  
  &lt;Directory "/path/to/extra-data"&gt;
      Options -Indexes -MultiViews +Includes
      AllowOverride None
      Order allow,deny
      Allow from all
  &lt;/Directory&gt;
  
  IndexOptions FancyIndexing HTMLTable SuppressHTMLPreamble
  AddType text/html .php .html .htm
  AddOutputFilter INCLUDES .php
  AddHandler application/x-httpd-php .php
  HeaderName "/~extra/HEADER.php"
  </code></pre>
  
  <p>My HEADER.php file:</p>
  
  <pre><code>&lt;?php
  
  if ( ! my_validate_cookie_function()) {
      header('HTTP/1.1 302 Found');
      header('Location: http://login.example.com/');
      exit(1);
  }
  </code></pre>
  
  <p>So, the header isn't sent to the browser. Setting Apache environment viariables doesn't seem to work, as they are long gone the moment HEADER.php is finished executing.</p>
  
  <p>The cookie itself is encrypted, hence need for PHP to validate it.</p>
  
  <p>Any suggestions how to achieve the desired effect?</p>