title: Apache DirectorySlash 301 with custom, content negotiated response body
body: <p>When Apache <code>mod_dir</code>'s <code>DirectorySlash</code> mechanism 301s a request that maps to a directory but that was missing its trailing slash, it produces a response body of type <code>text/html</code>, whatever the <code>Accept:</code> header of the request. While I understand that pretty much every client will just use the <code>Location:</code> header in the response to work out where to go, I'm wondering if it's possible to customise the response body to produce something other than <code>text/html</code>. In my specific case, I'm getting <code>application/rdf+xml</code> or <code>text/turtle</code> requests and returning like-encoded RDF responses. I'd like to be able to handle these missing-slash 301s with a response such as this:</p>
  
  <pre><code>@prefix owl: &lt;http://www.w3.org/2002/07/owl#&gt;.
  @base &lt;http://www.example.com/&gt;.
  
  &lt;resource&gt; owl:sameAs &lt;resource/&gt;.
  </code></pre>
  
  <p>I've tried using rewrite rules and <code>ErrorDocument</code> in the <code>.htaccess</code> file like this:</p>
  
  <pre><code>DirectorySlash off
  
  RewriteEngine on
  RewriteOptions AllowNoSlash
  
  # When no trailing slash, redirect to slashed path with a special ttl response body
  ErrorDocument 301 /resource-see-other.ttl
  RewriteCond %{HTTP:Accept} text/turtle
  RewriteCond %{REQUEST_URI} ^/resource$
  RewriteRule .* /resource/ [L,R=301]
  
  # When text/turtle request with trailing slash, return resource
  RewriteCond %{HTTP:Accept} text/turtle
  RewriteCond %{REQUEST_URI} ^/resource/$
  RewriteRule .* index.ttl [L]
  </code></pre>
  
  <p>This works OK. But it stops working as soon as I need to handle the cases for other content types. Although I can add another set of rewrite rules without any problems, I can't repeat the <code>ErrorDocument</code> directive in the <code>.htaccess</code> file; only the last such directive in the file will actually apply, resulting in all 301 responses getting the same response body.</p>
  
  <p>I suppose if I wasn't restricted to <code>.htaccess</code> I might be able to use <code>&lt;Location&gt;</code> directives which would isolate the <code>ErrorDocument</code>s from each other.</p>
  
  <p>So is there a way to do content negotiation on error response bodies?</p>