Created
June 26, 2019 17:21
-
-
Save OlegShchavelev/ba877f41360b606a6a8937491ac344ea to your computer and use it in GitHub Desktop.
Создаем плагин (имя на ваш вкус). На вкладке Системные события выбираем OnHandleRequest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*Переадресация с адресов, заканчивающихся без слеша / на адреса со слешем / */ | |
$url=$_SERVER['REQUEST_URI']; | |
if($_SERVER['HTTPS']=='on'){$http='https';}else{$http='http';} | |
if(substr($url,0,6)=='/index'){ | |
header("HTTP/1.1 301 Moved Permanently"); | |
header("Location: ".$http."://".$_SERVER['HTTP_HOST']); | |
exit(); | |
} | |
$exclude=array('.xml','.txt','.ico','.yml','json','.php','html','.pdf','.svg'); //массив исключений - какие адреса могут формироваться системой | |
$query_str=strstr($_SERVER['REQUEST_URI'],'?'); //Если в адресе есть параметры (например ?a=something123 ) | |
if($query_str!=''){ | |
$url=substr($url,0,strpos($url,$query_str)); //Отделяем параметры от адреса | |
} | |
if(!in_array(substr($url,-4), $exclude) and substr($url,-1)!='/' and stripos($url,'bannerclick')==0){ | |
$url=substr($url,1).'/'; //убираем / в начале и добавляем в конце | |
$obj=$modx->getObject('modResource',array('uri'=>$url)); | |
if(!$obj){ | |
header("HTTP/1.1 404 Not Found"); | |
}elseif($obj->get('published')==1){ | |
header("HTTP/1.1 301 Moved Permanently"); | |
header("Location: ".$http."://".$_SERVER['HTTP_HOST'].'/'.$url.$query_str); | |
exit(); | |
}else{ | |
header("HTTP/1.1 404 Not Found"); | |
exit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment