WebDevelopersNotes logo

home-icon Home / Answers / How can I have index.php as the index file?

How can I have index.php as the index file?

How can I have index.php as the index file?

My web server recognizes only index.html as the index file. Why? I want to use index.php because this way I can take advantage of all PHP features on the home page such as file includes. How can I do this without messing up anything?
Johnny Holl

This problem has a very simple solution. You just need to provide the appropriate instructions in the .htaccess file (that’s a period sign followed by htaccess). I am assuming you are running the Apache web server.

Sponsored Links

Checking and backing up the existing .htaccess file

First we need to check if an .htaccess file exists. Connect to your web server and move to the document root. Hunt for the file – it’s generally the first one. Download it to the local computer and make a backup so that you can quickly restore it in case things go wrong.

Open the file in Notepad or any basic text editor and check if there is a line that starts with DirectoryIndex as in:

DirectoryIndex index.html

Change it to:

DirectoryIndex index.php

this tells Apache to use index.php as the index file for the main directories and all its subdirectories.

You can also modify the line as:

DirectoryIndex index.html index.php

But in this case, if a directory has both index.html and index.php, Apache will use the former as the index file. You can also change the order.

Creating a new .htaccess file

The .htaccess file can easily be created in Notepad or any basic text editor. Open a blank document and put in the following line:

DirectoryIndex index.php

Save the file as .htaccess and upload it to the document root.

That’s it; index.php will now work as the index on all directories.

Set up any file as index

Any file can be made to act as the index for Apache. You just need to specify its name in the DirectoryIndex command in the .htaccess file. Thus:

DirectoryIndex openingpage.php

Please understand that the commands in .htaccess work for the directory is which the file is placed as well as all the subdirectories. Therefore, it’s a good practice to include more than one index file in DirectoryIndex. This tells Apache web server to fish out the second file in case the first one is missing. For instance,

DirectoryIndex openingpage.php index.php index.html

Apache will first try to load openingpage.php. If that is not present, the server will hunt for index.php and then index.html. If no files are found, Apache will throw up a 404 Page Not Found error.

Related articles

Sponsored Links

Your comments
Star icon IMPORTANT Have a question / problem? Click here to ask an expert.

Sponsored Links

Tips

How do I view a deleted web page?
Is there a way to view a deleted web page - one that is no longer available? Yes there is and the solution is quite simple. [more...]

Fact

The famous smiley was designed by Harvey Ball in 1963 to boost the morale of employees of Guarantee Mutual Company of Ohio. Ball, who was then a freelance artist, was paid $45 for his creation. [more...]

We use cookies to give you the best possible website experience. By using WebDevelopersNotes.com, you agree to our Privacy Policy