A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 year404 Error Page ~ Redirect an Image - PHP - page 1
User InfoPosts
404 Error Page ~ Redirect an Image - PHP#1
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\">";

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

?>

posted date: 2009-04-14 09:20:00


Re: 404 Error Page ~ Redirect an Image - PHP#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-14 09:20:01


Re: 404 Error Page ~ Redirect an Image - PHP#3
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.

posted date: 2009-04-14 09:24:00


Re: 404 Error Page ~ Redirect an Image - PHP#4
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_pathThis 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.

posted date: 2009-04-14 09:25:00


Re: 404 Error Page ~ Redirect an Image - PHP#5
Remember to die() after the header() call.

posted date: 2009-04-14 09:28:00


Re: 404 Error Page ~ Redirect an Image - PHP#6
How does this scale to thousands of files effectively? Using the PHP script the author could use a database backend...

posted date: 2009-04-14 09:32:00


Re: 404 Error Page ~ Redirect an Image - PHP#7
perfect - thanks.

posted date: 2009-04-14 09:40:00


Re: 404 Error Page ~ Redirect an Image - PHP#8
I never used the \r\n on my location redirects, and it still works fine.

posted date: 2009-04-14 09:42:00


Re: 404 Error Page ~ Redirect an Image - PHP#9
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.

posted date: 2009-04-14 09:47:00


Re: 404 Error Page ~ Redirect an Image - PHP#10
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: example.com\r\nContent-Type: text/plain");

posted date: 2009-04-14 09:51:00


Re: 404 Error Page ~ Redirect an Image - PHP#11
header ('HTTP/1.1 301 Moved Permanently'); header ('Location: '.$location);

posted date: 2009-04-14 09:54:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.