Set Your Directory Index (Homepage) Using .htaccess

Written by David Walsh on September 20, 2007 · 14 Comments

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

Comments

  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 htacess:

    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?

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

  2. 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 htacess:
    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?

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

  4. Or using this:

    DirectoryIndex index.htm index.php

  5. 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. 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. Thanks for not explaining the syntax at all. What is index.php, what is index.html, what is site-down.php?

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

Be Heard

Tip: Wrap your code in <pre> tags or link to a GitHub Gist!

Use Code Editor
Older
Checking For Leap Year Using PHP
Newer
Weekend Links - Accordion 2.0, curl.js, How To Be Happy At Work, Deadlines Kill Inspiration, Facial Hair, CSS3 Failure