简体   繁体   中英

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.

  • I'd prefer something that'll work site-wide in httpd.conf
  • I want to keep the with/without trailing-slash detection that mod_dir has.
  • My attempts using mod_rewrite tend to mess up the URI variables at the end :(

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM