catching all invalid URLs

廉价感情. 提交于 2020-01-03 15:56:46

问题


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!


回答1:


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

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



回答2:


in .htaccess in the web root

ErrorDocument 404 /yourFile.php



回答3:


Easiest would be to add a custom 404 redirect in htaccess.

Just add something like this:

ErrorDocument 404 /errors/404.php

to a .htaccess (create one if needed) in your application root. And all request to none extistent pages will be redircted to your own custom 404 page.



来源:https://stackoverflow.com/questions/4729655/catching-all-invalid-urls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!