Catch All Invalid URLs

I recently updated the site and almost all of the URLs have changed. I redirected them all (or I hope), but it is possible that some of them slipped. Is there a way to somehow catch all the invalid URLs and send the user to a specific page and somehow find out which URL the person came from so that I can register this and fix it? I think I can use .htaccess somehow, but I don’t know how to do it. I am using PHP. Many thanks!

+3
source share
3 answers

You can use a custom ErrorDocumenthandler written in PHP to catch URLs that have "slippable":

# .htaccess file
ErrorDocument 404 /not-found.php

And in not-found.php:

switch($_SERVER['REDIRECT_URL']) {
    case '/really_old_page.php':
        header('HTTP/1.1 301 Moved Permanently');
        header('Location: /new-url/...');
    /*  As suggested in the comment, exit here.
        Additional output might not be handled well and
        provokes undefined behavior. */
        exit;
    default:
        header('HTTP/1.1 404 Not Found');
        die('404 - Not Found');
}
+7

.htaccess -

ErrorDocument 404 /yourFile.php
+1

404 htaccess.

- :

ErrorDocument 404 /errors/404.php

in .htaccess (create it if necessary) in the root of the application. And all requests to any other pages will be redirected to your own 404 page.

0
source

Source: https://habr.com/ru/post/1786254/


All Articles