scriptygoddess

21 Jan, 2003

404 redirects

Posted by: kristine In: Scripts

I recently converted several sections of my site from html to php, and I wanted an easy way to change over without uploading a redirect page in the place of each html page. So I came up with this option.

Required:
Your server must allow you to use php pages for error messages.
If that's not the default, you'll need access to the htaccess file to make the change.
You will need to delete the .html files from the folders you are uploading .php files in their place.

For example, I have a folder at http://love-productions.com/about/. It previously had index.html, lp.html, k.html. Now it has index.php, lp.php, and k.php and I've deleted the html pages. So if you visit /about/index.html, it will redirect you to /about/index.php.

Here's what it does: A 404 page is what automatically comes up when the file isn't found. Using the server information for the link that was requested, I check to see if that URL has an .html extension. If so, then I check if a filename with the same name exists in that folder with a .php extension. If so, a meta tag redirects it to the new page. Otherwise, a normal "page not found" error is given. You'll want to customize the red letters below to have a link to your index page or search page.

<html>
<head>
<title>Page Not Found</title>

<?
//get the page searched for.
$root=$_SERVER["DOCUMENT_ROOT"];
$page = parse_url($_SERVER["REQUEST_URI"]);
$page=$page[path];

//find the extension of current page
$ext_array =explode(".",$page);
$last = count($ext_array) – 1;
$ext = $ext_array[$last];

if ($ext=="html") {
$ext = ".$ext";
$page=str_replace($ext,"",$page);
$extpage=$page.".php";
if (file_exists($root.$extpage)) {
echo "<meta http-equiv=\"Refresh\" content=\"1;url=", $extpage, "\">";
echo "<META name=\"robots\" content=\"NOINDEX, FOLLOW\">";
}
}
?>
</head>
<body>
<h1>Page Not Found</h1>
If possible, the browser will try redirecting you to the appropriate place. Otherwise, the file you clicked isn't found!
Try going to the <a href="http://yoursite.com">index page</a>
</body>
</html>

If you need to modify your server's htaccess file, you'll want to add this line to it so it recognizes the .php file instead of the normal 404 page:

ErrorDocument 404 /404.php

I haven't tested this on any other sites that mine, so it probably should be considered a beta script until several people test it out :)

17 Responses to "404 redirects"

1 | noel

January 21st, 2003 at 4:26 pm

Avatar

Couldn't you just have made it so that PHP's extension is .html on the server?

2 | photomatt

January 21st, 2003 at 5:00 pm

Avatar

You can also do something like this, which I used when converting a site from ASP to PHP. I may have gotten this from somewhere, I don't remember:

RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\.asp$ $1 [C,E=WasHTML:yes]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [S=1]
Rewrite Cond %{ENV:WasHTML} ^yes$
RewriteRule ^(.*)$ $.asp

Of course you need to have mod_rewrite installed, which most good hosts do. You can change the before and after extensions to be anything you like actually. However in my opinion the best solution and way to avoid the problem entirely is to use Apache's mod_negotiation/Multiviews to serve your pages sans extensions. The web server provides a wonderful layer of abstraction that provides a tremendous amount of flexibility, and truthfully why should your site's visitors even know from our files if you're using PHP or Python or SSI or CF to power your site? Extensions are an old OS artifact.

If anyone is wondering the code to make the above comment work (again on apache) is

AddType application/x-httpd-php .html .htm .php .whatever

3 | Kristine

January 21st, 2003 at 5:49 pm

Avatar

I could have used that — I know some of my friends here use that, but I didn't want to make my normal html pages have to look for php commands to be parsed, which could slow down the server.

I did look at mod_rewrite, but wasn't sure that it was the way to go since not all of my pages were being converted over. I'm definately not so familiar with htaccess, so thanks for posting that.

Thanks both of you for the alternatives.

4 | blake

January 21st, 2003 at 7:54 pm

Avatar

kristine, you are my queen. i have been searching for a way to do this since i converted to php to no avail – and there you are with the most simple solution that a guy like me (the type who sees "application/x-httpd-php" or "mod_rewrite" and starts crying) can use.

thanks for that – you rock!

5 | Meredith

January 22nd, 2003 at 10:56 pm

Avatar

All hail Kristine! ๐Ÿ˜€

Thank you thank you.

6 | taine

January 27th, 2003 at 3:19 pm

Avatar

The other option is to do what you are doing but actually have php do the redirect instead of the meta tag.

Something like:
$root=$_SERVER["DOCUMENT_ROOT"];
$page = parse_url($_SERVER["REQUEST_URI"]);
$page=$page[path];

//find the extension of current page
$ext_array =explode(".",$page);
$last = count($ext_array) – 1;
$ext = $ext_array[$last];

if ($ext=="html") {
$ext = ".$ext";
$page=str_replace($ext,"",$page);
$extpage=$page.".php";
if (file_exists($root.$extpage)) {
header('location: '.$extpage); }

This is NOT tested but should be close.

The trick here is to do this as the very first thing in the 404 file.

taine

7 | Nick Forsberg

August 7th, 2003 at 9:22 am

Avatar

I wanted to transfer any urls coming into a .com address across to the .co.uk site.
It sits in the error page on the .com site (which had been indexed by google etc…) then just converts the url to the .co.uk and redirects. If that page doesn't exsist then the sites error page turns up.
Have a play: http://www.tripsworldwide.com/news/

<html>
<head>
<title>New-Server</title>
<?php
//get the page searched for
$root=$_SERVER["DOCUMENT_ROOT"];
$page = parse_url($_SERVER["REQUEST_URI"]);
$page=$page[path];

//Convert to new server
echo "<meta http-equiv=\"Refresh\" content=\"1;url=http://www.new-server.co.uk$page\">";
echo "<META name=\"robots\" content=\"NOINDEX, FOLLOW\">"; ?>
</head>
<body bgcolor="black" leftmargin="0" marginheight="0" marginwidth="0" topmargin="0">
</body>
</html>

8 | sente

November 5th, 2003 at 4:21 pm

Avatar

Another, slightly different way of doing the same thing:

<?
//get the page searched for.
$root=$_SERVER["DOCUMENT_ROOT"];
$page = parse_url($_SERVER["REQUEST_URI"]);
$page=$page[path];

//find the extension of current page
$ext_array =explode(".",$page);
$last = count($ext_array) – 1;
$ext = $ext_array[$last];

$real404 = 1;

if ($ext=="html") {
$ext = ".$ext";
$page=str_replace($ext,"",$page);
$extpage=$page.".php";
if (file_exists($root.$extpage)) {
$real404 = 0;
include "$extpage";
}
}
if($real404) {
?>

<html>
<head>
<title>Page Not Found</title>
</head>
<body>
<h1>Page Not Found</h1>
If possible, the browser will try redirecting you to the appropriate place. Otherwise, the file you clicked isn't found!
Try going to the index page
</body>
</html>
<?
}
?>

9 | elise

January 4th, 2004 at 1:58 am

Avatar

Amazing. I can't believe how well this works. And how I, a non-techie, fairly newbie could get it to work without a hitch. Thank you Kristine.

10 | Bruce

April 18th, 2004 at 10:30 pm

Avatar

Great script, I have a serious question tho,
Is it possible to script a redirect if someone changes a filename from, example:
page_1.php to page-1.php
and have a redirect to the new page?
Similar to above script but instead of it looking for html extension it looks for underscores and changes them?
I have several people that would be really interested..
thanks

11 | kadyellebee

January 21st, 2003 at 3:43 pm

Avatar

404 redirects tutorial
Since I was asked how I did it (big smile at meredith!), I put together a tutorial for 404 redirects

12 | Axiomatic Breakfast

January 22nd, 2003 at 5:55 pm

Avatar

Links Links Links…
Just a couple of links I want to keep track of. OpenAntiVirus exiscan scriptygoddess

13 | me-me-me

September 23rd, 2003 at 12:38 pm

Avatar

404 redirect
if you changed the extension of the pages on your site, but didn't change any paths or names, this is a trick to get your error pages to redirect people to the php-page when they try to display the html-dito.

14 | Learning Movable Type

January 25th, 2004 at 11:13 pm

Avatar

Custom Error Messages
Error messages are the messages that are displayed when a visitor to your site encounters a server error. The most common error messages are 404: File Not Found and 403: Access Forbidden. The 404 error message is displayed when a…

15 | Priorities & Frivolities

April 9th, 2004 at 8:18 pm

Avatar

Comments Sections
In the past few days, P&F has been bombarded with spam — of the particularly offensive sort. So I've instituted a few technical changes:Each post's…

16 | Koldark's Komputer World

August 5th, 2004 at 6:03 pm

Avatar

404 Page
Thanks to scriptygoddess I have a little bit better 404 page. It is now more dynamic and will automatically redirect you from the .html page to the .php page if it exists. I have to do a few more modifications…

17 | Minimizing impact of domain change - Small Business Forum

December 23rd, 2006 at 4:28 am

Avatar

[…] Re: Minimizing impact of domain change I am not a technical person but I am told you can program a redirect using 404 and there is a 3 something redirect which I can recall but here is an article on the scriptygoddess ยป 404 redirects 404 redirect: […]

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements