2011-01-18 86 views
3

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

回答

7

你可以使用PHP编写的定制ErrorDocument处理器来捕获具有 “下滑了” 网址:

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

很大,这就是我一直在寻找。我想我可以用.htaccess这种方式捕捉404以外的其他错误吗?谢谢。 – Kentor 2011-01-19 15:23:09

1

在网站根目录的.htaccess

ErrorDocument 404 /yourFile.php 
0

最简单的将是添加自定义404重定向htaccess的。

只需添加这样的事情:

ErrorDocument 404 /errors/404.php 

到的.htaccess(如果需要创建一个)在您的应用程序根。并且所有请求都不会存在的页面将被重新传送到您自己的自定义404页面。