Change Default Directory Index Page Using .htaccess

By default whenever you build or design a site, the web server looks for certain name and types of page on your hosting directory. In most cases that should be something like index.html, index.htm, index.php, or default.html page. However, if you are running an apache server, you easily change it to something else. Let me explain it to you with an example.

Let’s assume, you have a domain name called “www.myname.com”. At the root directory of that site, you can create your .htaccess file and can write the following line on it.

DirectoryIndex index.html

What it means is that, as soon as someone visits your site(www.myname.com), the server will display the “index.html” page first. DirectoryIndex is the directive that defines/sets the resource that needs to be pulled first. Now if you want to change it to “first.html” page instead of “index.html” page, you can do it simply writing the following line.

DirectoryIndex first.html

In this case, server would be looking for “first.html” page and serve as soon as it is found. However, if you have different types of pages (i.e. index.html, index.php) on your server, you can mention them and prioritize them as well. Here is how you do it.

DirectoryIndex index.php index.html

As we are adding index.php page as our first choice, the server would be looking for index.php page first. If for some reason it can’t be found, only then the server would look for index.html page.

Missing Something?

Now, the thing that you need to remember is that DirectoryIndex directive on your .htaccess (at the root) file will be applied to all the folders and sub-folders as well. This may or may not be a problematic issue depending on what exactly you are looking for. Allow me to explain.

Let’s say, you have a folder on your web-server called “portfolio” and you are using the following line on your .htaccess file.

DirectoryIndex first.html

What will happen is that, when someone goes to this URL (www.myname.com/portfolio ), the server will still be looking for “first.html” page as the index page for the URL mentioned above. This is somewhat problematic, isn’t it? Well, here is the solution for you.

# index page for www.myname.com
DirectoryIndex first.html

# index page for www.myname.com/portfolio
<Directory "/portfolio">
   DirectoryIndex index.html
</Directory>

What you are doing here is simply defining separate index pages for sub-directories while having our custom index page for the root.

Note: Generally speaking, having one .htaccess file at the root directory is the best way to go. However, if you have another .htaccess file inside “portfolio” folder, it will supersede the one at the root directory.

Sources: mod_dir

Today In History

  •  
  •  

Comment

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from HTMLify, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.