MODX: 'noindex, nofollow'-Meta-Tag in der Produktionsumgebung automatisch entfernen

Schon mal beim Launch einer neuen Seite vergessen den 'noindex, nofollow'-Meta-Tag raus zu nehmen? Wirklich nicht? Ein Snippet sorgt dafür, dass das nicht vorkommen kann.

So lange eine Webseite noch nicht online ist, sollte sie einen 'noindex, nofollow'-Meta-Tag haben. Das gilt natürlich auch für Entwicklungs- und Stagingumgebungen, sofern diese von außen erreichbar sind. Das unten stehende Snippet sorgt dafür, dass der Tag auf der produktiven Seite verschwindet.

<?php
/*
 * Snippet "set_noindex_on_dev_subdomain"
 *
 * Returns <meta name="robots" content="noindex, nofollow" />
 * if a subdomain equals a certain value (like "develop" or "new"),
 * or a domain equals a defined domain.tld ("mydevserver.com").
 *
 * This snippet only works on subdomains. If the sites URI has no subdomain,
 * the snippet will exit disregarding any settings.
 * This is to make sure, your production site is not ignored by search engines...
 *
 * Properties:
 * excludesubdom = Optional, additional subdomain(s) to filter out (comma separated list).
 * excludedom = Optional, domain(s) to filter out (comma separated list).
 *
 * "noindex, nofollow" will be returned if one of the following conditions is met:
 * - The subdomain equals one of the default values in $subdomdef (see below),
 * - OR the subdomain equals 'excludesubdom', if set.
 * - The websites domain equals one of the default values in $domdef (see below),
 * - OR websites domain equals 'excludedom', if set.
 *
 * If the URI contains 2 two subdomains like "dev.subdomain.devdomain.tld",
 * $excludedom must be set to "subdomain.devdomain.tld" or &excludesubdom to "dev"
 * to return "noindex, nofollow".
 *
 * Usage:
 * [[set_noindex_on_dev_subdomain? &excludesubdom=`mydevsubdomain` &excludedom=`mydevserver.tld`]]
 *
 */
$output = '';

// default subdomains to exclude
// use like: array("develop","dev").
$subdomdef = array("develop","dev","local","loc","preview","prev","pre","next","new","test" );

// default domains to exclude.
// use like: array("mydevserver.com","mystagingserver.org").
$domdef = array();


// get subdomains to exclude
$excludesubdom = $modx->getOption('excludesubdom', $scriptProperties);
$excludesubdomar = array_map('trim', explode(',', $excludesubdom));
$subdomexcluded = array_merge($excludesubdomar, $subdomdef);

// get domains to exclude
$excludedom = $modx->getOption('excludedom', $scriptProperties);
$excludedomar = array_map('trim', explode(',', $excludedom));
$domexcluded = array_merge($excludedomar, $domdef);

// get the current domain
$url = $modx->context->getOption('site_url', null, 'default');
$parsedUrl = parse_url($url);
$host = explode('.', $parsedUrl['host']);
$hostitems = count($host);

// check if it's a subdomain, otherwise exit
if ($hostitems < '3') {
    return;
}

// check if domain meets excluded domains or default domains
if ($hostitems = '3') {
    $domain = "$host[1].$host[2]";
    if (in_array($domain, $domexcluded)) {
        $output = "<meta name=\"robots\" content=\"noindex, nofollow\" />";
    }
}
if ($hostitems = '4') {
    $domain = "$host[1].$host[2].$host[3]";
    if (in_array($domain, $domexcluded)) {
        $output = "<meta name=\"robots\" content=\"noindex, nofollow\" />";
    }
}

// check if domain meets excluded subdomain or default subdomains
$subdomain = $host[0];
if (in_array($subdomain, $subdomexcluded)) {
    $output = "<meta name=\"robots\" content=\"noindex, nofollow\" />";
}

return $output;