Community Support
January 09, 2009, 09:06:01 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Search Staff List Login Register  
Pages: [1]
  Print  
Author Topic: Custom 404 + key redirection  (Read 18 times)
Andrew
Byteact/i.create Administrator
Administrator
*****
Offline Offline

Hosting Plan: Premium Hosting
Operating System: Linux
Browser: Opera
Posts: 127
Referrals: 2



« on: October 29, 2008, 06:40:39 AM »

Okay, recently we redesigned Byteact.com, here's a little bit of 404-fu for you...

Below is the standard text for the 404 file, let's say... errors/404.php
Quote
<?php 
$request
=$_SERVER['REQUEST_URI']; 

$arrMoved=array("/path/oldname.php"=>"/path/newname.php"
"/old_path/oldname.php"=>"/new_path/newname.php"); 

if(
array_key_exists($request,$arrMoved)) 

$newplace="http://".$_SERVER['HTTP_HOST'].$arrMoved[$request]; 
header("HTTP/1.0 301 Moved Permanently"); 
header("Location: $newplace"); 
header("Connection: close"); 
exit(); 

else 

header("HTTP/1.0 404 Not Found"); 


?> 

<!-- For 404 errors that are not redirected to new places, pop your standard 404 error coding below -->

<html> 
<head> 
<title>404 Error page</title> 
</head> 
<body> 
<p>Sorry but this page isn't here.</p> 
</body> 
</html>

To modify it and make changes to match your site edit the lines:

$arrMoved=array("/path/oldname.php"=>"/path/newname.php"
"/old_path/oldname.php"=>"/new_path/newname.php"); 

To add files you've moved, let's say... I moved /dog.html to /dog.php so I would modify the first line between the quotes to read "/dog.html"=>"/dog.php", and of course if you have more than one redirect you could always add new lines after the first one, just make sure the last one doesn't have a , at the end and has ); after the last quote.

Next you would modify the HTML to add your custom error page coding...

Quote
<html>
<head>
<title>My Site - 404 File Not Found</title>
</head>
<body>
<p>Dude, you are lost! No worries, come back to the main site and you should be able to find what you're looking for: <a href="#">homepage</a></p>
</body>
</html>

Then finally, upload it to your website (say in /errors/404.php) and edit your .htaccess to point to it:

Code:
ErrorDocument 404 /errors/404.php

What it does...

If you moved a page and you want to keep SEO, you add it to the redirection array at the top to redirect the old links to the new location, say dog.html to the .php equivalent (though if you just changed extensions there's MOD_REWRITE rules you could/should use) whereas if they're after say... somereallyoldlink.html that you never replaced, your 404 error would come up.
Logged

Unsolicited private messages for support issues will go unanswered
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.7 | SMF © 2006-2008, Simple Machines LLC | Sitemap | Archive Valid XHTML 1.0! Valid CSS!