Set Your Directory Index (Homepage) Using .htaccess

By  on  

As every web developer knows, the server default for a directory's default file is index.{extension}. Apache is usually set to index.php or index.html. It's a good practice to set your DirectoryIndex setting in your .htaccess file, especially if you're hosting your website on shared hosting. A hosting provider's guidelines could change at any time but your .htaccess file's directive would prevent any problem for you.

Another reason to use this property would be if your website were to go down for maintenance, or a section of your website. Here's how you can use this property.

The Code

DirectoryIndex index.php index.html site-down.php

Recent Features

  • By
    Introducing MooTools Templated

    One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating: new Element Madness The first way to create UI-driven...

  • By
    CSS vs. JS Animation: Which is Faster?

    How is it possible that JavaScript-based animation has secretly always been as fast — or faster — than CSS transitions? And, how is it possible that Adobe and Google consistently release media-rich mobile sites that rival the performance of native apps? This article serves as a point-by-point...

Incredible Demos

Discussion

  1. Hello, I have 2 homepages (in the root folder): 1 in French (let’s say bienvenue.html), and 1 in English (let’s say welcome.html).
    If i set the English page as default via DirectoryIndex, then Google (.fr) Search Results will display the English (meta) contents – Welcome blablabla – when there’s a match with French search terms…

    I also have these 2 lines in htaccess:

    Rewritecond %{HTTP_HOST} ^nicobalivillas\.com
    RewriteRule ^(.*)$ http://www.nicobalivillas.com/$1 [R=301,L]
    

    Could this be because of this redirection?
    Any hint on how to fix this so that the French version is displayed for search results matches in French?

  2. Thanks it works great! :) God bless you…

  3. Or using this:

    DirectoryIndex index.htm index.php

  4. That code is an seo friendly way of redirecting your domain to the www version.

    For displaying results in French it might be best to display the site in a folder named something like /FR. Read this post by seomozfor more information.

  5. cross

    hello m working on a website on which now some pages are coming like page.php?cid=1&pid=5. but in database the category name for id 1 is say demo and the product name for id 5 is say xyz so i want to rewrite my url as page.php/demo/xyz.html. how to do this? please help. is this possible through htaccess file?

  6. cross

    sory the url sud b like “page/demo/xyz.html” or “page/demo/xyz”

  7. That’s the one line i was looking for as i want to leave index.html but want to use index.php. Thanks!

  8. Alex

    Thanks for not explaining the syntax at all. What is index.php, what is index.html, what is site-down.php?

    • @Alex – If you don’t know what those file extensions are, then you need to do something else instead of showing your ignorance to people trying to learn.

  9. I have a HTML website with a issue:

    Some of my web pages appear duplicate:

    domain.com/folder/

    domain.com/folder/index.html

    How to resolve this? plz guide.

  10. Kenny

    I have a issue:

    I have a webpage welcome.php
    I would like to use .htaccess to write the url into welcome.php?target=abc
    How do I do it?

  11. NB

    HI!
    I have a flash splash page which has a link “http://www.my url.com/subfolder/index.html”. Now I have a Joomla site, I need to redirect the html address to “http://www.my url.com/subfolder/index.php” instead. How do I add the line in .htaccess? I tried to use the redirect in control panel but it did not work. Thank you!

  12. tom

    thanks! was trying to find how to set a home page of another php file with htaccess, and found your post.

  13. Question Is :
    hello m working on a website on which now some pages are coming like page.php?cid=1&pid=5. but in database the category name for id 1 is say demo and the product name for id 5 is say xyz so i want to rewrite my url as page.php/demo/xyz.html. how to do this? please help. is this possible through htaccess file?

    sory the url sud b like “page/demo/xyz.html” or “page/demo/xyz”

    Answer Is : write in yr .htaccess file as follows.

    RewriteEngine On
    RewriteRule ^/?([a-zA-Z-_0-9]+)?/?([a-zA-Z-_0-9]+)?/?$ index.php?cate=$1&prod=$2 [L]
    

    the first one (?([a-zA-Z-_0-9]+)?) is for demo and second one (?([a-zA-Z-_0-9]+)?) is for xyz which will be passed to index.php?cate & prod

    so cate=demo and prod=xyz and use this arguments in yr index.php file by $_GET["cate"] and $_GET["prod"]

    Regards ,
    Mohammad Ziyad

  14. Hi
    I’m trying to fix a friends websites. Root domain with independent sub-domains. First website uploaded fine to correct subdomain folder, but second site will only work if loaded into the root directory and other sites now do not work. I think I may have deleted off the .htaccess directory. What do I do?

  15. Just to complement your entry, I used somenthing like

    DirectoryIndex maintenance.php

    inside a directory named ./maintenance

    So it goes ahead and loads the maintenance.php page. No need mentioning any “index.*” page.

  16. Hello

    Can I change page name of index.html and replace with any keyword page name like keyword.html.
    And that set into directoryindex in htaccess file.

  17. How about set error 404 page, can you help me to create .htaccess??
    Thanks for help

    • In the .htaccess add the following code:

      errorDocument 404 /404.htm

      Then create the error doc, 404.htm, in the home directory (or include the file path in the .htaccess file).

  18. Sean

    In usual, when page loads, the index.php file is displayed on user’s display, but I have a task to make a new homepage, which should be displayed to user. The question is, how can I accomplish the task, so that the user will see newly created homepage index.php? If I need to configure .htaccess, how should I do it?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!