This little snippet allows you to list all the domains that you may have enabled in your Apache set-up. I decided to create this code when I was tidying up files on my PC and thought that this was a good way to check what content and site was still alive. There are a couple of things to note/change in the script, but it should run almost as is.
They way I have my virtual hosts set-up on my Apache server at home is to have a separate .conf file that only includes virtual hosts. In my main httpd.conf I include the .conf file like so: Include conf/<name>.conf. This way, if I add or remove a <VirtualHost> I don't need to go through the whole main configuration file. The code can read the original file if you want it too, but this is a more efficient way in my opinion.
The script then loops through the file as an array, checking every line for "ServerName". I have found when setting up virtual hosts on Windows that ServerName is a vital element in the proceedings. An example of a virtual host entry from my <name>.conf would be as follows:
<VirtualHost *>
ServerAdmin webmaster@domain.pc
DocumentRoot "X:/home/user/domain.pc"
ServerName domain.pc
ServerAlias domain.pc
</VirtualHost>
If ServerName is found in that line, then the item is modified to get the domain name and turn it into a URL. I hope you find this code useful.
<?php
$file = 'path/to/file';
$lines = file($file);
$list = '<ul>'."\n";
foreach ($lines as $line_num => $line) {
if ( strpos($line, 'ServerName') !== FALSE ) {
$ServerName = trim( str_replace('ServerName', '', $line) );
$list .= '<li><a href="http://'. $ServerName .'" title="'. $ServerName .'">'.
$ServerName .'</a></li>'."\n";
}
}
$list .= '</ul>'."\n";
echo $list;