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
    Regular Expressions for the Rest of Us

    Sooner or later you'll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In...

  • By
    Write Better JavaScript with Promises

    You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...

Incredible Demos

  • By
    CSS :target

    One interesting CSS pseudo selector is :target.  The target pseudo selector provides styling capabilities for an element whose ID matches the window location's hash.  Let's have a quick look at how the CSS target pseudo selector works! The HTML Assume there are any number of HTML elements with...

  • By
    Styling CSS Print Page Breaks

    It's important to construct your websites in a fashion that lends well to print. I use a page-break CSS class on my websites to tell the browser to insert a page break at strategic points on the page. During the development of my...

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!