title: apache mod_session in rewrite rules
body: <p>I would like to read the contents of the environment variable <code>HTTP_SESSION</code> created by <code>mod_session</code> and rewrite some urls depending on the user stored in it. But it seems I don't have access to it, even if I can see the contents in the CGI script.</p>
  
  <p>I have a login form:</p>
  
  <p><code>cat /var/www/login/login.html</code></p>
  
  <pre><code>&lt;head&gt;
      &lt;meta charset="utf-8"&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form method="POST" action=""&gt;
          Username: &lt;input type="text" name="httpd_username" value="" /&gt;
          Password: &lt;input type="password" name="httpd_password" value="" /&gt;
          &lt;input type="submit" name="login" value="Login" /&gt;
      &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>i created a testuser:</p>
  
  <p><code>htpasswd -c /etc/apache2/passwords testuser</code></p>
  
  <p>and the corresponding apache configuration:</p>
  
  <pre><code>&lt;VirtualHost *:80&gt;
          ServerName simon.test
          DocumentRoot /var/www/
  
          RewriteEngine On
  
          LogLevel debug session:trace8 session_cookie:trace8 headers:trace8 rewrite:trace8
  
          Session On
          SessionCookieName session path=/;domain=simon.test;httponly;
          SessionEnv On
  
          &lt;Location /login/&gt;
                  Require all granted
          &lt;/Location&gt;
  
          &lt;LocationMatch ^/(sp)/(.+)$&gt;
                  ErrorDocument 401 "/login/login.html"
  
                  AuthType form
                  AuthName "authenticationform"
  
                  AuthFormProvider file
                  AuthUserFile /etc/apache2/passwords
  
                  AuthName "test"
  
                  AddHandler cgi-script .sh
                  Options +ExecCGI
  
  
                  SessionEnv On
  
                  RewriteRule .* - [E=test:thisworks]
                  RewriteRule .* - [E=THE_SAVED_SESSION:%{ENV=HTTP_SESSION}]
  
                  Require valid-user
          &lt;/LocationMatch&gt;
  &lt;/VirtualHost&gt;
  </code></pre>
  
  <p>in <code>/var/www/sp/i.sh</code></p>
  
  <pre><code>#!/bin/bash
  
  echo "Content-type: text/plain"
  echo ""
  
  
  env
  
  exit 0
  </code></pre>
  
  <p>when i go to <code>http://simon.test/sp/i.sh</code> the login form is shown, and the login works, but the <code>THE_SAVED_SESSION</code> environment variable is never shown. Even in the apache rewrite debug logs it is empty, but the output of the CGI script shows its contents correctly.</p>
  
  <p>Does someone know why, and how I can access the contents of that variable?</p>