Skip to content

Instantly share code, notes, and snippets.

@ngyuki
Created August 1, 2014 12:43
Show Gist options
  • Save ngyuki/26c8be4bdaaee852718c to your computer and use it in GitHub Desktop.
Save ngyuki/26c8be4bdaaee852718c to your computer and use it in GitHub Desktop.
vhost.php
<?php
/**
* Apache mod_vhost_alias のバーチャルホストの一覧を表示する
*
* ex. httpd.conf
*
* LoadModule vhost_alias_module modules/mod_vhost_alias.so
*
* <VirtualHost *:80>
* ServerName vhost.example.net
* ServerAlias *.example.net
* VirtualDocumentRoot /var/www/vhost/%1/html
* </VirtualHost>
*/
// vhost_alias のドキュメントルート
$vhost_alias_path = '/var/www/vhost/*';
// vhost_alias のドメイン名 (未指定ならサーバのホスト名)
$vhost_domain = null;
//---
if ($vhost_domain === null)
{
$vhost_domain = php_uname('n');
}
if (strlen($vhost_domain))
{
$vhost_domain = "." . $vhost_domain;
}
$level = null;
$arr = explode('/', $vhost_alias_path);
foreach ($arr as $i => $dir)
{
if ($dir === '*')
{
$level = $i;
break;
}
}
if ($level === null)
{
exit;
}
$vhost_list = glob($vhost_alias_path);
$vhost_list = array_map(function($path) use ($level) {
$arr = explode('/', $path);
if (isset($arr[$level]) === false)
{
return null;
}
else
{
return $arr[$level];
}
}, $vhost_list);
$vhost_list = array_filter($vhost_list);
?>
<html>
<head>
<style>
body {
padding: 16px 32px;
}
h1 {
font-style: italic;
border-bottom: 1px solid #000;
margin: 0 -8px;
padding: 0 8px;
}
ul {
font-size: 120%;
}
li {
margin: 4px 0;
}
a {
color: #00f;
text-decoration: none;
}
a:hover,
a:active {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>VirtualHost Aliases</h1>
<ul>
<? foreach($vhost_list as $dir): ?>
<li>
<a href="http://<?= htmlspecialchars($dir . $vhost_domain) ?>/"><?= htmlspecialchars($dir) ?></a>
</li>
<? endforeach; ?>
</ul>
<i>Date: <?= date('Y-m-d H:i:s') ?></i>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment