Skip to content

Instantly share code, notes, and snippets.

@staabm
Last active November 19, 2015 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save staabm/65277a39d2cb8b015870 to your computer and use it in GitHub Desktop.
Save staabm/65277a39d2cb8b015870 to your computer and use it in GitHub Desktop.
create a index over all registered vhosts of the local apache2 webserver
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style>
<!--
ul {
float: left;
width: 300px;
margin-top: 10px;
padding-left: 10px;
}
ul li {
list-style-type: none;
}
h2 {
max-width: 280px;
overflow: hidden;
margin-top: 5px;
}
label {
float: right;
font-size: 1.1em;
}
-->
</style>
</head>
<body>
<label>
<input type="checkbox" id="show_aliases" />
Show aliases
</label>
<?php
exec('/usr/sbin/apachectl -S', $outLines);
$outLines = preg_grep('{^\s+(port \d+ namevhost|alias)}', $outLines);
$outLines = array_map('trim', $outLines);
$knownDomains = array();
$knownAliases = array();
foreach($outLines as $line) {
if (strpos($line, 'namevhost') !== false) {
list(, $port,, $domain, $confPath) = explode(" ", $line);
// before: (/path/to/etc/my_vhost.conf:38)
// after: /path/to/etc/my_vhost.conf
$confPath = trim($confPath, '()');
$confPath = preg_replace('{[:0-9]+$}', '', $confPath);
$scheme = 'http';
if($port == 443) {
$scheme = 'https';
}
// make sure we dont override https-urls with http urls, but the opposite is ok
if (isset($knownDomains[$confPath][$domain])) {
if ($port == 443) {
$knownDomains[$confPath][$domain] = $scheme . '://'. $domain;
}
} else {
$knownDomains[$confPath][$domain] = $scheme . '://'. $domain;
}
// apache2.2 does not provide aliases, parse those out of the config files
if (strpos($_SERVER['SERVER_SOFTWARE'], 'Apache/2.2') !== false) {
foreach(file($confPath) as $confLine) {
// skip comments and non-ServerAlias lines
if (stripos($confLine, 'ServerAlias') === false || preg_match('/^\s*#/', $confLine)) {
continue;
}
list(, $aliasDomain) = explode(" ", trim($confLine));
$knownAliases[$domain][] = $aliasDomain;
}
}
} else {
list(, $aliasDomain) = explode(" ", $line);
$knownAliases[$domain][] = $aliasDomain;
}
}
foreach($knownDomains as $confPath => $domainData) {
echo '<ul>';
echo '<h2 title="'. $confPath .'">'. basename($confPath) .'</h2>';
$aliases = array();
foreach($domainData as $domain => $url) {
echo '<li><a href="'. $scheme . '://'. $domain .'">'. $domain .'</a></li>'."\n";
if (isset($knownAliases[$domain])) {
$aliases += $knownAliases[$domain];
}
}
foreach(array_unique($aliases) as $aliasDomain) {
echo '<li class="alias"><a href="http://'. $aliasDomain .'">Alias: '. $aliasDomain .'</a></li>'."\n";
}
echo '</ul>';
}
?>
<script>
function hideAliases() {
var aliases = document.querySelectorAll(".alias");
for (var alias of aliases) {
alias.style.display = "none";
}
}
function showAliases() {
var aliases = document.querySelectorAll(".alias");
for (var alias of aliases) {
alias.style.display = "inline-block";
}
}
document.querySelector("#show_aliases").addEventListener("click", function() {
if (this.checked) {
showAliases();
} else {
hideAliases();
}
});
hideAliases();
</script>
</body></html>
@staabm
Copy link
Author

staabm commented Nov 19, 2015

Alternativ: http://blog.brynmosher.com/2011/08/03/apache-one-liner-to-list-all-server-names-and-aliases/
Funktioniert auch mit apache 2.2+

$ THECONFS="`/usr/sbin/apachectl -S 2>&1 | grep -oE '\/[^\:]+' | grep -vE '^\s*#'`"; for ACONF in ${THECONFS}; do cat ${ACONF} | tr '\t' ' ' | grep -E '^ *(ServerName|ServerAlias)' | sed -r 's/^( |ServerName|ServerAlias)+//g' | tr ' ' '\n'; done | sort -fu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment