List Apache Virtual Hosts

September 02, 2008

Just a quick post with a handy bit of code I wrote this morning to list all the virtual hosts you've got defined in your Apache conf file. This lets you build a menu with links to all the running sites on your Apache server.

Here's the code....

<cfset vhosts = FileRead( ExpandPath('../apache/conf/extra/httpd-vhosts.conf') ) />

<cfset aServerNames = ReMatchNoCase( "\n[ \t]*ServerName[^\n]+", vhosts ) />

<cfoutput>
<ul>
<cfloop array="#aServerNames#" index="index">
  <cfset servername = ListLast(index, " #Chr(09)#") />
  <li><a href="http://#servername#/">#servername#</a></li>
</cfloop>
</ul>
</cfoutput>

Note that my virtual hosts are configured in the file 'httpd-vhosts.conf' so you may need to change this depending on your server setup. This script requires ColdFusion 8, but it shouldn't be too hard to convert it for older verisions.

It's really simple but great it you run lots of sites (in my case it's for my local development server). It is worth noting that I've set it to ignore any servernames that are commented out.

I hope this is useful to someone :)


No comments

Leave a comment

If you found this post useful, interesting or just plain wrong, let me know - I like feedback :)

Please note: If you haven't commented before, then your comments will be moderated before they are displayed.