apachemod-rewritedirectory-listingmod-autoindex

Use mod_rewrite to create custom directory index in Apache


So, Apache's default directory listing sucks and mod_autoindex's customisation options suck, so I figured I could write some rewrite rules in my server root to redirect any requests to a directory:

<IfModule rewrite_module>
    RewriteEngine on
    RewriteCond %{REQUEST_URI} -d
    RewriteRule .* /index.php?dir=$0
</IfModule>

Now, I get the impression this works at least a little, as turning off indexes still leaves localhost accessible (it displays the index.php), however it doesn't seem to reach subdirectories (which either revert to Apache's directory indexes if the indexes option is on, or give a 403 if they're off).

My question is: can I get this rule to apply globally or is my quest for pretty directory indexes doomed to failure?

Edit: I should note: the above rules are contained in a .htaccess in the server root.

Edit: Ideally the solution, if it exists, would still have DirectoryIndex functionality (that is, index.php etc. will be displayed if it exists),


Solution

  • The -d directory test does only work with absolute file system paths. So either provide an absolute file system path (e.g. by using %{DOCUMENT_ROOT}%{REQUEST_URI} or %{REQUEST_FILENAME} directly) or use -D that makes an additional subrequest to resolve the URI path to a file system path. I’d prefer:

    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule .* /index.php?dir=$0