Search code examples
apache.htaccessdirectoryindex

Can you change the DirectoryIndex of specific directories?


I know that you can use DirectoryIndex example.html in .htaccess, but what I need is to change the index for specific directories, like so:

DirectoryIndex /ex/index_file.html

or

DirectoryIndex /ex2/index_file2.html

Is there a way that you can accomplish this? I don't want to rename the file I'm trying to do this for to index.html because that's not what it is. You are required to login first, and if you're not it will redirect you; but I think that looks sloppy.


Solution

  • You can create an .htaccess in each directory that you want a different directory index in. The .htaccess in the ex directory should contain:

    DirectoryIndex index_file.html
    

    But the on in ex2 should contain:

    DirectoryIndex index_file2.html
    

    You can also use <Directory> in the .htaccess in your web root as long as you are allowed to override this:

    <Directory ex>
       DirectoryIndex index_file.html
    </Directory>
    
    <Directory ex2>
        DirectoryIndex index_file2.html
    </Directory>