How to Make Apache Serve Index.PHP Instead of Index.Html

How to make Apache serve index.php instead of index.html?

As others have noted, most likely you don't have .html set up to handle php code.

Having said that, if all you're doing is using index.html to include index.php, your question should probably be 'how do I use index.php as index document?

In which case, for Apache (httpd.conf), search for DirectoryIndex and replace the line with this (will only work if you have dir_module enabled, but that's default on most installs):

DirectoryIndex index.php

If you use other directory indexes, list them in order of preference i.e.

DirectoryIndex index.php index.phtml index.html index.htm

index.php not loading by default

Apache needs to be configured to recognize index.php as an index file.

The simplest way to accomplish this..

  1. Create a .htaccess file in your web root.

  2. Add the line...

DirectoryIndex index.php

Here is a resource regarding the matter...

http://www.twsc.biz/twsc_hosting_htaccess.php

Edit: I'm assuming apache is configured to allow .htaccess files. If it isn't, you'll have to modify the setting in apache's configuration file (httpd.conf)

Why does index.html have priority over index.php?

It really depends on the Server you're using. This is a matter of configuration. It's not that there's any advantage from using html vs php filetype.

You could say that the .html variation takes precedence due to the fact that it's the most basic web format.

If you're using Apache, just check the default .htaccess setup:

DirectoryIndex index.html index.shtml index.php index.htm default.html Default.htm default.html Default.html default.shtml Default.shtml page1.html index.pl index.cgi index.php3 index.phtml home.htm home.html home.shtml index.wml

You can edit that and make it fit your needs.

when to use index.php instead of index.html

You will have to choose the PHP extension (.php) when you want php code to be executed in the file. PHP code is code between the opening <?php or <? and the closing ?> tags.

When no PHP code should be executed you can use the .html extension.

Usually when using the .php extension you are telling the web server, that it should use a php interpreter to process the file before it will be delivered to the browser. The php interpreter will then replace all content between the <?php and ?> by the output of the PHP code. Just as if you wrote it manually. The processed file will then be delivered to the browser.

However, using the .php extension to tell the web server to process php code is configurable. If you want you can use other file extensions too.

There is another thing that should be pointed out. When you only type the url path (without a filename) like :

http://www.myserver.com/

there is an order of extensions (filenames) which the webserver (apache) searches for an index document. For example an apache config may contain a section like:

<IfModule mod_dir.c>
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>

Meaning that the index document is searched in the order above. This means if you place an index.html and a index.php in the same folder - and having the configuration above - always the index.html would be delivered by the server.

Serve index.php if index.html is not present in directory without a trailing slash

The DirectoryIndex document lists the files to try to serve when requesting a directory. You don't need mod_rewrite for this.

However, to use the DirectoryIndex when you set DirectorySlash Off you need to manually append the trailing to the directory with an internal rewrite, otherwise, a 403 will be triggered even when that directory contains a DirectoryIndex document. (It is for this reason that mod_autoindex needs to be disabled to prevent accidental disclosure of information - the presence of a DirectoryIndex document is not sufficient to prevent the auto-generated directory listings when DirectorySlash Off is set.)

For example:

# Disable directory listings (mod_autoindex)
Options -Indexes

ErrorDocument 404 /error/error404.html

# Look for index.html then index.php in the directory being requested
DirectoryIndex index.html index.php

# Prevent mod_dir appending the trailing slash
DirectorySlash Off

RewriteEngine On

# If request a directory without a trailing slash then rewrite to append it
# This allows DirectoryIndex to work as intended
# - exclude the document root
RewriteCond $1 !/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.+) $1/ [L]

Make sure the browser cache is cleared. mod_dir issues a 301 (permanent) redirect to append the trailing slash.


UPDATE#1:

/blog/ page is a WordPress page with its own .htaccess.

The problem with this is that when the rewrite engine is enabled in the subdirectory then it completely overrides the mod_rewrite directives in the parent config, so the slash is not appended and the directory index is not triggered (and the mod_rewrite directives are not processed because of the missing slash). (This feels a bit chicken and egg.)

The only way I have successfully resolved this is to move the WordPress directives into the parent config (making the necessary changes) and remove /blog/.htaccess altogether.

For example, append the following modified WordPress directives after the directives above:

# WordPress in "/blog" subdirectory
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteRule ^blog/index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/. /blog/index.php [L]

UPDATE#2:

have URL with trailing slash to redirect to URL without trailing slash? like for example, if I visit /about/ it should redirect to /about?

Yes, we can do this. Since we are removing the trailing slash from everything, including directories, this is essentially unconditional. The only caveat is that the rule to remove the trailing slash must only apply to direct requests from the client and not rewritten requests by the later rewrite, that appends the trailing slash to directories, which would otherwise result in a redirect loop.

Aside: You obviously need to ensure that all your internal links do not include the trailing slash, otherwise, the user will experience an external redirect (bad for SEO and your server).

We can do this by checking against the REDIRECT_STATUS environment variable, which is empty on the initial request, and set to 200 (as in 200 OK status) after the first successful rewrite (that appends the trailing slash to directories).

For example, the following should go immediately after the RewriteEngine directive, before the existing rewrite:

# Redirect direct requests to remove trailing slash from all URLs
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.+)/$ /$1 [R=302,L]

Test first with a 302 (temporary) redirect and change to a 301 (permanent) redirect once you have confirmed it works as intended. This is to avoid potential caching issues.

Summary

With all the directives in place:

# Disable directory listings (mod_autoindex)
Options -Indexes

ErrorDocument 404 /error/error404.html

# Look for index.html then index.php in the directory being requested
DirectoryIndex index.html index.php

# Prevent mod_dir appending the trailing slash
DirectorySlash Off

RewriteEngine On

# Redirect direct requests to remove trailing slash from all URLs
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.+)/$ /$1 [R=301,L]

# If request a directory without a trailing slash then rewrite to append it
# - This allows DirectoryIndex to work as intended
# - Exclude the document root
RewriteCond $1 !/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.+) $1/ [L]

# WordPress in "/blog" subdirectory
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteRule ^blog/index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/. /blog/index.php [L]

How do I change the default index page in Apache?

I recommend using .htaccess. You only need to add:

DirectoryIndex home.php

or whatever page name you want to have for it.

EDIT: basic htaccess tutorial.

1) Create .htaccess file in the directory where you want to change the index file.

  • no extension
  • . in front, to ensure it is a "hidden" file

Enter the line above in there. There will likely be many, many other things you will add to this (AddTypes for webfonts / media files, caching for headers, gzip declaration for compression, etc.), but that one line declares your new "home" page.

2) Set server to allow reading of .htaccess files (may only be needed on your localhost, if your hosting servce defaults to allow it as most do)

Assuming you have access, go to your server's enabled site location. I run a Debian server for development, and the default site setup is at /etc/apache2/sites-available/default for Debian / Ubuntu. Not sure what server you run, but just search for "sites-available" and go into the "default" document. In there you will see an entry for Directory. Modify it to look like this:

<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>

Then restart your apache server. Again, not sure about your server, but the command on Debian / Ubuntu is:

sudo service apache2 restart

Technically you only need to reload, but I restart just because I feel safer with a full refresh like that.

Once that is done, your site should be reading from your .htaccess file, and you should have a new default home page! A side note, if you have a sub-directory that runs a site (like an admin section or something) and you want to have a different "home page" for that directory, you can just plop another .htaccess file in that sub-site's root and it will overwrite the declaration in the parent.

index.php is not loading by default in apache2

This is likely a caching issue in your browser when you go to http://localhost/ try pressing Ctrl + F5

How to make apache serve file:///

You can't make Apache serve file:///. Using that scheme instructs the browser to fetch the file directly from the filesystem. If you want to use Apache then you have to use http:// (or another URL scheme that makes a network request that Apache supports).



Related Topics



Leave a reply



Submit