handle logic for missing index file in Apache

If you visit a url like http://domainhostseotool.com, Apache will append an index file to the url such as  http://domainhostseotool.com/index.php before further processing. The index file is specified with the DirectoryIndex directive. You may find the following DirectoryIndex in your virtualhost configuration:

DirectoryIndex index.html index.htm index.php index.asp index.aspx index.jsp

This behavior is not restricted to the document root url. It is also applied for other sub-directories. So if you visit  http://domainhostseotool.com/dir/, Apache will append index to that url as well: http://domainhostseotool.com/dir/index.php. What if that index file does not exist? You may think apache will return “404 not found”. However, the fact is Apache returns “403 forbidden” instead of “404 not found”. There is a discussion about it on stackoverflow.  So if there is no index.php under /documentroot/dir/, you will see the following message in browser:


You don’t have permission to access /dir/ on this server.

This information is not obtained from some file but generated by Apache internally. You can specify an error document to override this:

ErrorDocument 403 /_errorpages/403.html

Now, you will see the content from http://domainhostseotool.com/_errorpages/403.html on your browser. Note that /_errorpages/403.html in the ErrorDocument  directive is url path, not file system path

What happens if /documentroot/_errorpages/403.html is also missing? You will see the following error:


You don’t have permission to access /dir/ on this server.

Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.

Note that there is only one 403 response despite the message says “Additionally, a 403 Forbidden error was encountered …”

The default configuration of Apache has a special error document for the root url (as seen in /etc/httpd/conf.d/welcome.conf):

So it you visit http://domainhostseotool.com(or http://domainhostseotool.com/, or http://domainhostseotool.com//,etc.), and there is no index file in the document root, you will see the 403 page with the content from /usr/share/httpd/noindex/index.html. Note the pattern in the LocationMatch matches against the path part of request url. Since this LocationMatch  is outside any virtual host, the 403 document will apply to all virtual hosts.  Note also that in order to make the 403 document available, the permission is granted in the <Directory> section. The Alias directive maps a path part of url to a file system path.

The handling of missing index occurs before entering into .htaccess, so the rewrite rules in .htaccess have no chance to redirect url like http://domainhostseotool.com/dir/(also refer to this). The url http://domainhostseotool.com/dir/index.php (even documentroot/dir/index.php does not exist), however, can be captured by the rewrite rules in .htaccess, thus can be redirected.

See another post about this issue.


Posted in tips of hosting