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.


12 thoughts on “Change default directory page using .htaccess

  1. 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.

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

  3. hem

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

  4. 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….

  5. hem

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

  6. 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

  7. 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….

  8. HmmGreat

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

  9. wow…nice to hear that maan…

  10. vishwa

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

  11. SAM

    can you give me links where i can learn seo .

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

Leave a Reply

Your email address will not be published. Required fields are marked *