捕获所有无效的网址

Ken*_*tor 4 php url redirect url-rewriting

我最近升级了一个网站,几乎所有网址都已更改.我已经重新定向了所有这些(或者我希望如此),但有些可能已经被我滑倒了.有没有办法以某种方式捕获所有无效的URL并将用户发送到某个页面,并以某种方式知道该人来自哪个URL,以便我可以记录这个,并修复它们?我想我可以用某种方式使用.htaccess但不确定如何.我正在使用PHP非常感谢!

Lin*_*een 7

您可以使用ErrorDocument用PHP编写的自定义处理程序来捕获"滑过"的URL:

# .htaccess file
ErrorDocument 404 /not-found.php
Run Code Online (Sandbox Code Playgroud)

并在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');
}
Run Code Online (Sandbox Code Playgroud)