apachemod-rewritedirectoryindex

Dynamic DirectoryIndex (Regex DirectoryIndex?)


In Apache, is it possible to have a dynamic/regex DirectoryIndex Directive?

Normal DirectoryIndex would do:

server/DIRECTORY1 --> server/DIRECTORY1/index.html
server/DIR1/DIR2  --> server/DIR1/DIR2/index.html
...
etc

But how would I accomplish:

server/DIRECTORY1 --> server/DIRECTORY1/DIRECTORY1.html
server/DIR1/DIR2  --> server/DIR1/DIR2/DIR2.html
...
etc

I want this because I have many index files open in my editor, and it's hard to tell the tabs apart because they're all named index.


Solution

  • You can put this code in httpd.conf (mod_rewrite block)

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteCond %{DOCUMENT_ROOT}/$1/$2/$2\.html -f
    RewriteRule ^/?(.+?)/([^/]+)/?$ /$1/$2/$2.html [L]
    
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteCond %{DOCUMENT_ROOT}/$1/$1\.html -f
    RewriteRule ^/?([^/]+)/?$ /$1/$1.html [L]