Change default directory page using .htaccess


I know you guzy all know about index file. This is the default file which is executed in the web server. For example, if you type”” in this the browser, the “index.php” gets loaded first. This “index.php” file is commonly know as directory index file.

Basically , the default executed file in the web directory starts with “index” such as “index.php”, “index.htm”, “index.html”, “index.aspx” etc.

Last time in one server, I’ve faced a problem. “index.html” as getting executed by default but “index.php” as not getting executed.In such kind of scenario, we may need to change the default file to be executed in the web directory.

Changing Default Directory index page using .htaccess

The default directory index pages are defined in the apache’s configuration file and it can be override from .htaccess file. To do so put the following code in the .htaccess file,

DirectoryIndex index.html index.htm index.php index.cgi

Remember that you can also append the other names of files in the DirectoryIndex directive and the order of the above “DirectoryIndex” directive determines the order of index file in web directory.

First of all, “index.html” file is looked inside directory and if that file is not found then “index.htm” file is looked and if that is also not found then again seek for “index.php” and so on. If any file defined above directive can’t be found in the directory, then web server just list the files inside that directory if the directory listing is enabled. You can also prevent the directory listing using .htaccess file.


Tags: ,

Enter your email address and get free tutorials, tips and tricks of PHP, Ajax, JavaScript and CSS directly delivered to you email inbox:

12 Comments on “Change default directory page using .htaccess”

  • Asim Suvedi wrote on 24 April, 2008, 1:26

    Its really nice to have a nepali blogger on the net with expertise in programming field.
    I am a newbie in php but i am familiar with the basics even though i have not worked in php for long.
    I have a question for you:
    I have a lot of images in a folder named ‘images’ inside (public_html).
    Now i want to change the name of the images and then put them into a new folder ‘titles’. Folder ‘titles’ is not present. It has to be created.
    Can you help me in this case? Please try it once.

  • Roshan wrote on 24 April, 2008, 4:43

    Ok asim sure…..I’ll write a post for how to do this…

  • hem wrote on 24 April, 2008, 4:52

    cant this be done simply from a FTP client.. from client u can creat a new folder just like u do in windows..

  • Roshan wrote on 24 April, 2008, 8:33

    Hi hem….thanks for the suggestion….and it really worked using ftp client(I’m using WS-FTP Pro)…I was able to copy and paste the whole directory…but one problem was I was only able to copy and paste not cut and paste. I think other ftp clients may allow cut+paste.

    I was thinking do the cut+paste operation in PHP though….

  • hem wrote on 24 April, 2008, 18:40

    if cut paste didn’t work .. u can copy then delete the original foler.. or whatever u copied… :-)

  • Larry Soule wrote on 25 April, 2008, 19:10

    Hi – I found some code that you made to export info in a database to an excel spreadsheet.

    I have info in a data base that I need downloaded to a textnote. Before it puts the info into the text note I need a script that will create a directory either on the desktop or a specific folder on my computer. The textnote goes into this folder. Last there is a URL link in the database with the persons picture, I need that put into the folder that was created.

    So folder creation -> then textnote with database info into folder -> last picture in the folder created

    any help would be greatly appreciated. I can get things into the database no problem and I can also display them and format it into a webpage, but I just don’t know how to download the info.

    the display page is

  • Roshan wrote on 26 April, 2008, 19:17

    As far as my knowledge is there….you can’t create a directory in the client’s desktop computer using PHP, you can only create a directory in server….

  • maan wrote on 17 June, 2008, 13:32


    I really want this one. i am exploring .htaccess for url rewriting . And this catagory (.htaccess) helps me a lot.

  • Roshan wrote on 17 June, 2008, 16:09

    wow…nice to hear that maan…

  • vishwa wrote on 19 March, 2009, 10:39

    this is vishwa will u please gude me in brife what is htaccess file with simple code example…!

  • SAM wrote on 8 November, 2009, 15:20

    can you give me links where i can learn seo .

  • Didactory wrote on 9 March, 2012, 13:41

    Thanks for this information.
    How do you set the index page inside a subfolder? Is it:
    DirectoryIndex /folder/index.html
    Thanks again

Write a Comment


Copyright © 2015 Roshan Bhattarai's Blog. All rights reserved.
Powered by, Custom Theme and Calling Card Company.