In init.php you can determine that url will return 404?

In init.php there is a function which is processing the url. If the url has been changed, is a 301 redirect to the changed url. The processor is connected like this: AddEventHandler("main","OnProlog","redirect301");
You need at the end of this function to check for the existence of the page, and then do a 301 redirect or 404.
In Bitrix you can test the url?
March 19th 20 at 09:03
2 answers
March 19th 20 at 09:05
Solution
As I decided. Can someone come in handy.
AddEventHandler("main","OnProlog","checkUri");
function checkUri(){
 $GLOBALS["STATUS"] = 200;
 $uri = $_SERVER['REQUEST_URI'];
 //processing the url
 if(strcmp($uri, $_SERVER['REQUEST_URI']) !== 0){
 $_SERVER['REQUEST_URI'] = $uri;
 $GLOBALS["STATUS"] = 301;
}
}
}

AddEventHandler('main', 'OnEpilog', 'сһеск404Еггог', 1);
сһеск404Еггог function() {
 if ((defined('ERROR_404')) && (ERROR_404 == 'Y') && ($_SERVER['REQUEST_URI'] != '/404.php') || preg_match('/[a-za-z\s]+/msi', $_SERVER['REQUEST_URI'])) {
 $GLOBALS ['STATUS'] = 404;
}
}

AddEventHandler("main","OnEpilog","redirects");
function redirect(){
 if($GLOBALS["STATUS"] === 301) {
 header("HTTP/1.1 301 Moved Permanently");
 header('Location:' . $_SERVER['REQUEST_URI']);
exit();
 } elseif($GLOBALS["STATUS"] === 404){
 global $APPLICATION;
$APPLICATION->RestartBuffer();
 include $_SERVER['DOCUMENT_ROOT'] . '/bitrix/templates/'.SITE_TEMPLATE_ID.'/header.php';
 require ($_SERVER["DOCUMENT_ROOT"] . "/404.php");
 include $_SERVER['DOCUMENT_ROOT'] . '/bitrix/templates/'.SITE_TEMPLATE_ID.'/footer.php';
}
}
March 19th 20 at 09:07
Hi!
If the page does not exist by default so it'll work on a redirect 404.php

But if on the forehead, then do a request to this url and see the headers
$file = 'http://www.domain.com/somefile.jpg';
$file_headers = @get_headers($file);
if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
 $exists = false;
} else {
 $exists = true;
}


or curl option
function url_exists($url) {
 if (!$fp = curl_init($url)) return false;
 return true;
}


https://stackoverflow.com/questions/2280394/how-ca...

Find more questions by tags Processing URLhtaccess1C-Bitrix