April 20, 1998
DR. WEBSITE:
Beating the '404 File Not Found' Message; Formats for FTP Files
By David Fiedler and Scott Clark
Dear Dr. Website®:
How do you edit what the "404 file not found" document says?
You can do even better than that. You can force the server to return any file you want. This is good, because the infamous 404 display is a bit obscure for most Web surfers. There are two ways to do it:
As an individual Web developer, you have to edit a file called .htaccess in the directory where your HTML files are stored (often called public_html, and not always the same as your home directory). If it doesn't exist, editing it will create it (and don't forget the leading period). If you're on Unix, the file should be set to a mode of 644 using this shell command:
chmod 644 .htaccess
The contents of the .htaccess file should be simply the single line:
ErrorDocument 404 http://www.yourdomain.com/new404page.html
or whatever URL you want, so you can design your own custom 404 page. One trick you can use is to point this URL at the default home page (or search page) for your site, so that anyone attempting to access a bad URL will go there:
ErrorDocument 404 http://homerecording.com/index.html
If you're setting the 404 document as a Unix administrator for a whole site or a number of virtual Web servers running on one machine, you'll have to edit the httpd.conf file, generally found in /www/conf. The syntax is basically the same in this file, and generally looks something like this:
<VirtualHost www.webdeveloper.com>
DocumentRoot /www/webdev/wd
ServerName www.webdeveloper.com
ErrorDocument 404 /404page.html
Dear Dr. Website®: I need to know how to FTP files in a format that ensures that they are read from my HTML. For example, in my HTML I have links and images set up like this: <IMG SRC="/images/graphic.gif"> and <A HREF="food/vegetable.html">. How many directories deep should I go? What is the easiest way to do markup so that I can send whole folders to the server? Could you please explain in detail? How do you set yours up?
Well, a lot depends on how big your site is. On my smaller personal sites, I keep all media in their own directories and all HTML files in the main directory. So it looks something like:
.htaccess 404.html greetings.html index.html
images: dove.gif dragon.jpg glassart.jpg
sounds: batcall.wav blues.mid
WebDeveloper.com is another story, with almost 1,500 files, but we try to keep things simple there too.
We keep most "global" graphics in an /img directory, easily accessible from anywhere (there are subdirectories for different types of graphics, such as banners, buttons, and icons). We try to keep all Java classes, etc., in a single place as well. Graphics that are only accessed by one article are kept in the same directory with that article.
The most important thing is to always access files from a fixed location. For instance, our Dr. Website articles are kept in a directory called /drweb off the main directory. But we always reference them like <A HREF="/drweb/drwebsite042098.html">. Using the leading slash ensures that we know we're starting from that directory; if you say something like "drweb/drwebsite042098.html" the URL will fail if you move the file.
The worst solution is relative addresses--i.e., "../../drdummy/lostfile.html". That looks really impressive to novices, but it's guaranteed to confuse anyone, even the original author. Just try to figure out where that file came from at a later date!
Dear Dr. Website®: In your April 6 issue, on page 24, you incorrectly state that it is impossible, without the use of forms or CGIs, to include the body of a message in an e-mail link. This is incorrect, as the following example will illustrate:
<A HREF="mailto:eric@hisdomain.com?subject=Dr.EricWebsite&body=Hi Website">this is an e-mail with a message</A>
Eric was not the only reader to correct our misconception. While this technique does work, keep in mind that it only works for the 4+ browsers. It does gracefully downgrade, with the older browsers simply ignoring the body message, but you should not depend on it if your target audience may still be using older Web browsers or external mailers. The only completely sure way to handle e-mail is with a well-written CGI program.
Dear Dr. Website®: The example onClick sample shown in the 4/6/98 issue returns an error when using Microsoft Internet Explorer 4.0. My use of it is as follows:
<A HREF="http://www.sun.com" onclick="openit('http://www.sun.com') ;return false;">test link</a>
Where is the error occurring?
When we ran the question/answer in that issue, we were referring to the example from the previous column. The openit() function referred to in the above example needs to be present in the HEAD of the document:
<SCRIPT LANGUAGE="JavaScript">
<!--//
function openit(sURL){
newwindow=window.open(sURL,"newwin",
"scrollbars=yes,toolbar=no,directories=no,menubar=no,
resizable=yes,status=yes,width=600,height=500");
}
//-->
</SCRIPT>
This allows the function to gracefully degrade; an older browser simply opens the page within the browser as it normally would with any other link.
Another reader presented a simple addition to the HTML that would make the link work for even more browsers. Simply use a TARGET in the A HREF tag pointing to the same window that you're trying to open, in this case "newwin,":
<A HREF="http://www.sun.com" TARGET="newwin"
onclick="openit('http://www.sun.com');
return false;">test link</a>