PHP Page Redirect Array

One-file script to make subdirectory requests redirect to different URLs. Requires PHP 5.4 or greater.

Note: This is best kept to cases of protected/limited usage. For public or high usage, choose a more advanced script.

Script

<?php

/* The is the functionality for the redirect */
class UrlRedirect {
    private $fallback;
    private $addresses;

    /* Necessary for creating object */
    public function __construct() {}

    /* Applies definition of fallback address */
    public function fallback($string) {
        $this->fallback = $string;
    }

    /* Applies definitions for slugs and their urls */
    public function addresses($array) {
        $this->addresses = $array;
    }

    /* Retrieves the actual slug from the input */
    /* This is a very elementary form of "sanitization" */
    public function slug($string) {
        return strtolower(preg_replace("[^A-Za-z0-9_\-]","",$string));
    }

    /* Retrieves which url to use */
    public function url($slug) {
        return ((array_key_exists($slug, $this->addresses)) ? $this->addresses[$slug] : $this->fallback);
    }

    /* Does the redirect */
    public function redirect($slug = NULL) {
        header("Location:" . $this->url($slug));
        echo "Redirecting to " . $this->url($slug);
    }
}

/* Initialize functionality */
$redirect = new UrlRedirect();

/* Define address to use if address slug is missing or not valid */
$redirect->fallback("http://www.google.com");

/* Define address slugs and their associated urls */
$redirect->addresses([
            "yahoo"     => "http://www.yahoo.com",
            "bing"      => "http://www.bing.com",
]);

/* The actual request response */
if (isset($_GET["s"])) {
    $slug = $redirect->slug($_GET["s"]);
    $redirect->redirect($slug);
} else {
    $redirect->redirect();
}
?>

Example Route Configs

Apache .htaccess

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^s/([a-zA-Z0-9\-_]+) redirect.php?s=$1 [NC,L]
RewriteRule ^s redirect.php [NC,L]

Nginx Location

location /s {
    rewrite /s/(.*) https://erythisis.com/redirect.php?s=$1 permanent;
}