views:

806

answers:

3

I use a custom 404 page in a PHP application.

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.

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).

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\"&gt;";

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

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

?>
+1  A: 

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.

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.

Csaba Kétszeri
+5  A: 

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.

Note: headers must be sent before any other output

adam
Remember to die() after the header() call.
Pies
perfect - thanks.
OneNerd
I never used the \r\n on my location redirects, and it still works fine.
GoatRider
The \r\n isn't needed here, and shows adam is or knows perl developers :). The header() function handles creating each line of headers for you, and won't let you insert additional lines. Try this to see what I'm talking about header("Location: http://example.com/\r\nContent-Type: text/plain");
Alan Storm
header ('HTTP/1.1 301 Moved Permanently');header ('Location: '.$location);
OIS
Thanks - always forget that \r\n is a legacy throwback. The 301/2 mentioned by OIS and Csaba is an important part too
adam
A: 

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:

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.

Pras
How does this scale to thousands of files effectively? Using the PHP script the author could use a database backend...
X-Istence
True, it could, in which case the config file full of redirects could be generated only once for all the thousand redirects and never have to hit the database again. Since most older redirects will be static, you won't have to regenerate the list too often.
Pras