404错误页面〜重定向图像 - PHP

时间:2022-10-09 21:07:46

I use a custom 404 page in a PHP application.

我在PHP应用程序中使用自定义404页面。

On that 404 page, I check for certain 'known' older pages that no longer exist, and then redirect user to newer or most relevant current page. Works great.

在该404页面上,我检查某些不再存在的“已知”旧页面,然后将用户重定向到更新或最相关的当前页面。效果很好。

Now I am faced with a series of old images that have been removed, and am looking for a way to redirect the images to a new image (all inside of the php code if possible).

现在我面临一系列已被删除的旧图像,并且正在寻找一种将图像重定向到新图像的方法(如果可能的话,在PHP代码内部)。

I have hunted around briefly and came up empty.

我曾短暂地追捕并空出来。

Any way to do this?

有什么办法吗?

Here is a sample of my code:

以下是我的代码示例:

<?php
    //-- grab info regarding bad request --
    $root       = $_SERVER['DOCUMENT_ROOT'];
    $page       = parse_url($_SERVER['REQUEST_URI']);
    $page       = $page['path'];
    $referer        = $_SERVER['HTTP_REFERER'];
    $host       = $_SERVER['REMOTE_HOST'];

    //-- try to redirect old pages/files ----------------------
    //
    $page = urlencode($page);

    if ( stristr($page, "some_old_file.zip") ) {
        // Example file redirect
        echo    "<META HTTP-EQUIV=\"refresh\" CONTENT=\"0;URL=http://www.site.com/the/new/file.zip\">";

    } elseif ( stristr($page, "some_old_page.php") ) {  
        // example webpage redirect
        echo    "<META HTTP-EQUIV=\"refresh\" CONTENT=\"0;URL=http://www.site.com/the/new/page.php\">";

    } elseif ( stristr($page, "some_old_image.jpg") ) {
        // not sure how to do this ...
        // ...
        // ...
        // ...
        // ...
        // ...
        // not sure how to do this ...          
    } else {
        // everything else - direct to custom 404 search page
        echo    "<META HTTP-EQUIV=\"refresh\" CONTENT=\"0;URL=http://www.site/com/the/custom/404_help.php?page={$page}\">";
    }
    //
    // -------------------------------------------------------

?>

3 个解决方案

#1


instead of outputting a meta refresh, use a location header

而不是输出元刷新,使用位置标头

header("Location: /path/to/image.jpg\r\n");

The \r\n is just a new line to delimit the headers.

\ r \ n只是一个用来分隔标题的新行。

Note: headers must be sent before any other output

注意:标头必须在任何其他输出之前发送

#2


You will need server side redirect. Look around php function "header" and http status code 301 (moved permanently). You'll find a ton of ready made 5 liner solutions.

您将需要服务器端重定向。查看php函数“header”和http状态代码301(永久移动)。你会发现大量现成的5衬里解决方案。

If I were you, I'd use this method for html content too to inform the search engines about the new location of the same content.

如果我是你,我也会将此方法用于html内容,以便向搜索引擎通知相同内容的新位置。

#3


My recommendation would be to do this inside Apache using Apache redirects (in your Apache conf files or .htaccess). You can do something like this:

我的建议是在Apache内部使用Apache重定向(在Apache conf文件或.htaccess中)执行此操作。你可以这样做:

RedirectMatch old_file_path new_file_path

This will not only improve your performance but you won't have to do a lookup each time. And the best part is that it will be manageable should you need to add more missing redirects in the future.

这不仅可以提高您的性能,而且每次都不必进行查找。最好的部分是,如果您将来需要添加更多丢失的重定向,它将是可管理的。

#1


instead of outputting a meta refresh, use a location header

而不是输出元刷新,使用位置标头

header("Location: /path/to/image.jpg\r\n");

The \r\n is just a new line to delimit the headers.

\ r \ n只是一个用来分隔标题的新行。

Note: headers must be sent before any other output

注意:标头必须在任何其他输出之前发送

#2


You will need server side redirect. Look around php function "header" and http status code 301 (moved permanently). You'll find a ton of ready made 5 liner solutions.

您将需要服务器端重定向。查看php函数“header”和http状态代码301(永久移动)。你会发现大量现成的5衬里解决方案。

If I were you, I'd use this method for html content too to inform the search engines about the new location of the same content.

如果我是你,我也会将此方法用于html内容,以便向搜索引擎通知相同内容的新位置。

#3


My recommendation would be to do this inside Apache using Apache redirects (in your Apache conf files or .htaccess). You can do something like this:

我的建议是在Apache内部使用Apache重定向(在Apache conf文件或.htaccess中)执行此操作。你可以这样做:

RedirectMatch old_file_path new_file_path

This will not only improve your performance but you won't have to do a lookup each time. And the best part is that it will be manageable should you need to add more missing redirects in the future.

这不仅可以提高您的性能,而且每次都不必进行查找。最好的部分是,如果您将来需要添加更多丢失的重定向,它将是可管理的。