Y
Y
Yudgin8882019-03-28 18:02:24
1C-Bitrix
Yudgin888, 2019-03-28 18:02:24

How can I determine in init.php that the url will return 404?

In init.php there is a function in which the url is processed. If the url has been changed, a 301 redirect is made to the changed url. The handler is connected like this: AddEventHandler("main","OnProlog","redirect301");
At the end of this function, you need to check for the existence of the page, and then do a 301 or 404 redirect.
How can I check the URL in Bitrix?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
Y
Yudgin888, 2019-03-29
@Yudgin888

How I solved it. Maybe someone will come in handy.

AddEventHandler("main","OnProlog","checkUri");
function checkUri(){
    $GLOBALS["STATUS"] = 200;
    $uri = $_SERVER['REQUEST_URI'];
 //обработка урла
        if(strcmp($uri, $_SERVER['REQUEST_URI']) !== 0){
            $_SERVER['REQUEST_URI'] = $uri;
            $GLOBALS["STATUS"] = 301;
        }
    }
}

AddEventHandler('main', 'OnEpilog', 'сheck404Error', 1);
function сheck404Error() {
    if ((defined('ERROR_404')) && (ERROR_404 == 'Y') && ($_SERVER['REQUEST_URI'] != '/404.php') || preg_match('/[а-яА-Я\s]+/msi', $_SERVER['REQUEST_URI'])) {
        $GLOBALS["STATUS"] = 404;
    }
}

AddEventHandler("main","OnEpilog","redirects");
function redirects(){
    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';
    }
}

E
eternalfire, 2019-03-28
@eternalfire

Hey!
If the page does not exist, by default, the redirect to 404.php will work anyway.
But if in the forehead, then make a request to this url and look at 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...

6
6elkka, 2017-01-06
@EgoRusMarch

In the first variant

if (a[i] == key) tmp++; 
if (a[i] != key) tmp = 0; 
if (tmp > result) result = tmp;

replaced by
if (a[i] == key) tmp++; 
else if(tmp)
{
    if (tmp > result) result = tmp;
    tmp = 0;    
}

and at the end (after the loop) add
Then there will be no extra assignments and the code remains similar to the original version

C
CityCat4, 2017-01-05
@CityCat4

The concept of "better" is abstract and does not exist outside the phrase "better than". By what signs to evaluate the code - by speed, by readability, by another hundred-thousand different criteria?
If you want to evaluate the performance - translate the code into assembler and see the result. The speed of execution of assembler commands is also, of course, in parrots, but at least there you can compare in numbers.
If you want to evaluate the understandability of the code, the second one is much more difficult to perceive. In the first, by the way, the second if is completely superfluous :) - a[i] can either be equal to key or not equal, there is no third option, so it can be completely replaced by else - this will give a win in terms of speed.
Speaking of assignments. Operations like tmp++ may well be done in registers, in one command.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question