index.php files ignored

Ryan Schmidt ryandesign at
Sun Mar 20 03:22:37 PDT 2011

On Mar 19, 2011, at 18:34, Tony Miller wrote:

>>> I guess 'redirect' is the wrong term. It is really just going to the default virtual host on the server because it is not finding the index.php file. 
>> I agree it's probably using a different virtual host, but not because it's not finding the index.php file. Virtual hosts are independent environments. Apache does not decide, when it cannot find a file in one virtual host, to go looking for it in another.
> My understanding is apache will use the default virtual host for the server if it can't find a defined DirectoryIndex file. 

No, it's easy enough to demonstrate that's not how it works.

Here's how it works:

If a request comes in for a hostname or IP address for which there is a virtual host defined, then that virtual host is used. Otherwise, the main host (the one outside any VirtualHost block) is used.

If there is a directive for any particular setting inside a chosen VirtualHost block, then that directive takes effect. Otherwise, the corresponding directive from the main host is used.

So if we have:

DocumentRoot /www/main/htdocs
DirectoryIndex index.html

<VirtualHost *:80>
	DocumentRoot /www/foo/htdocs
	DirectoryIndex index.php

If a request comes in for, the main host takes effect and the user is sent the file /www/main/htdocs/index.html. If a request comes in for, and if also maps to this server and does not have a virtual host defined, the main host takes effect in that case too and the user gets the same file.

If a request comes in for, then that virtual host takes effect. Its directives for DocumentRoot and DirectoryIndex override those of the main host. So the user gets sent /www/foo/htdocs/index.php (after it is evaluated by mod_php). If /www/foo/htdocs/index.php does not exist, the user receives a 403 Forbidden error (if directory listings are off), or the directory listing (if directory listings are on). The server makes no attempt to locate or serve an index.html, because the only directive mentioning index.html has been overridden by this virtual host to no longer mention index.html.

If you wanted the server to serve index.php if it exists, and index.html otherwise, you would write:

DirectoryIndex index.php index.html

The fact you're getting a 404 Not Found, instead of a 403 Forbidden or a directory listing, is interesting... perhaps the entire directory it's looking for is not there, or has insufficient permissions.

>> What is the full path to this directory?
> /opt/local/apache2/htdocs/leopoldheritage

>> I guess the virtual host is not set up correctly. Could you show the Apache virtual host definition for
> <VirtualHost>
>  ServerName
>  ServerAlias
>  DocumentRoot "/opt/local/apache2/htdocs/leopoldheritage"
>  DirectoryIndex index.php

That seems correct to me, assuming is in fact the IP address that resolves to. (It does not resolve to anything for me, so I guess it's internal for you). To simplify, you can just put "*" instead of the actual IP address, which also future-proofs you against future IP changes.

>  ErrorLog "logs/"
>  CustomLog "logs/" common
> 	<IfModule mod_ssl.c>
> 		SSLEngine Off
> 		SSLCertificateFile "/etc/certificates/Default.crt"
> 		SSLCertificateKeyFile "/etc/certificates/Default.key"
>        SSLProtocol all -SSLv2
>        SSLCipherSuite "ALL:!ADH:RC4+RSA:+HIGH:!MEDIUM:!LOW:!SSLv2:!EXP"
> 	</IfModule>

SSL directives don't belong in this virtual host because this virtual host is defined to operate on port 80, which is for non-encrypted http traffic. SSL-encrypted http traffic (https) would occur on port 443, for which you would make a second virtual host.

> 	<IfModule mod_dav.c>
> 		DAVLockDB "/var/run/davlocks/.davlock100"
> 		DAVMinTimeout 600
> 	</IfModule>
> 	<IfModule mod_mem_cache.c>
> 		CacheEnable mem /
> 		MCacheSize 4096
> 	</IfModule>
> 	<Directory "/opt/local/apache2/htdocs/web5">

Should this directory be /opt/local/apache2/htdocs/leopoldheritage?

>        AllowOverride None
> 		<IfModule mod_dav.c>
> 			DAV Off
> 		</IfModule>
> 		Options All -Includes -ExecCGI +MultiViews -Indexes
> 	</Directory>
> 	<IfModule mime_module>
> 	  AddType html/text	php

This AddType for php should be deleted.

> 	  AddType application/x-httpd-php	php
> 	  AddType application/x-httpd-php-source	phps

The extensions should begin with a period:

	  AddType application/x-httpd-php	.php
	  AddType application/x-httpd-php-source	.phps

> 	</IfModule>
>  <IfModule mod_rewrite.c>
> 		RewriteEngine On
> 		RewriteCond %{REQUEST_METHOD} ^TRACE
> 		RewriteRule .* - [F]
> 	</IfModule>

Looks like this mod_rewrite is here to prevent a particular kind of cross-site attack:

You may want that block in the main part of your Apache configuration file, so it can benefit all your hosts, not just this one.

> </VirtualHost>

Check also the access log and error log for clues. When you try to access your hostname, and it says 404 not found, look in the error log to see exactly what file it looked for and couldn't find.

More information about the macports-users mailing list