简体   繁体   English

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

[英]404 Error Page ~ Redirect an Image - PHP

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. 在该404页上,我检查了某些不再存在的“已知”旧页面,然后将用户重定向到较新或最相关的当前页面。 Works great. 效果很好。

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}\">";
    }
    //
    // -------------------------------------------------------

?>

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 注意:标头必须在任何其他输出之前发送

You will need server side redirect. 您将需要服务器端重定向。 Look around php function "header" and http status code 301 (moved permanently). 看看php函数“标头”和http状态代码301(永久移动)。 You'll find a ton of ready made 5 liner solutions. 您会发现大量现成的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内容使用此方法,以将相同内容的新位置通知搜索引擎。

My recommendation would be to do this inside Apache using Apache redirects (in your Apache conf files or .htaccess). 我的建议是使用Apache重定向(在Apache conf文件或.htaccess中)在Apache内部执行此操作。 You can do something like this: 您可以执行以下操作:

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. 最好的部分是,如果将来您需要添加更多丢失的重定向,它将可以管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM