<?
$needRedirect = false;
$statusCode = 301;
$statusMessage = "Moved Permanently";

$fullUrl = $_SERVER['REQUEST_URI'];
$url = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

if (($_SERVER['REQUEST_METHOD'] === 'GET' || $_SERVER['REQUEST_METHOD'] === 'HEAD')) {
    //Исключить админку
    if (substr($url, 0, 8) === '/bitrix/') {
        return;
    }

    //Редирект со знака вопрос
    if (substr($fullUrl, -1) === '?') {
        $needRedirect = true;
        $new_url = str_replace('?', '', $fullUrl);
    }

    //Редирект много слешей
    if(preg_match('#//#', $fullUrl)){
        $needRedirect = true;
        $new_url = preg_replace('/(\/+)/','/', $fullUrl);
    }

    //Редирект на слеш
    if (substr($url, -1) !== '/' && !pathinfo($fullUrl, PATHINFO_EXTENSION)) {
        $needRedirect = true;
        $new_url = $url . '/';
    }

    if ($needRedirect) {
        header("Location: //{$_SERVER["SERVER_NAME"]}{$new_url}", true, $statusCode);
        header("Status: {$statusCode} {$statusMessage}");
        exit;
    }
}
