捕获所有无效的 URL [英] catching all invalid URLs

查看:50
本文介绍了捕获所有无效的 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I recently upgraded a site and almost all URLs have changed. I have redirected all of them (or so I hope) but it may be possible that some of them have slipped by me. Is there a way to somehow catch all invalid URLs and send the user to a certain page and somehow know what URL the person came from so I could log this, and fix those? I'm thinking I could use .htaccess somehow but am not sure how. I am using PHP Thanks so much!

推荐答案

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

You could use a custom ErrorDocument handler written in PHP to catch URLs having "slipped by":

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

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');
}

这篇关于捕获所有无效的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆