if((s->argc > 1) && !strcmp(s->argv[1], "directory"))
        pat->type = PT_DIR;
+    else if((s->argc > 1) && !strcmp(s->argv[1], "notfound"))
+       pat->type = PT_NOTFOUND;
+    else
+       pat->type = PT_FILE;
     sl = s->lno;
     while(1) {
        getcfline(s);
     return(NULL);
 }
 
-struct pattern *findmatch(char *file, int trydefault, int dir)
+struct pattern *findmatch(char *file, int trydefault, int type)
 {
     int i, o, c;
     char *bn, *ln;
                ln = file;      /* This should only happen in the base directory. */
        }
        for(pat = cfs[c]->patterns; pat != NULL; pat = pat->next) {
-           if(!dir && (pat->type == PT_DIR))
-               continue;
-           if(dir && (pat->type != PT_DIR))
+           if(pat->type != type)
                continue;
            for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
                if(rule->type == PAT_BASENAME) {
        }
     }
     if(!trydefault)
-       return(findmatch(file, 1, dir));
+       return(findmatch(file, 1, type));
     return(NULL);
 }
 
 
 {
     struct child *ch;
     struct config *ccf;
-    char *tmp;
+    struct pattern *pat;
     
-    tmp = sstrdup(path);
-    ch = findchild(tmp, ".notfound", &ccf);
-    if(childhandle(ch, req, fd, chinit, ccf?ccf->path:NULL))
-       childerror(req, fd);
-    free(tmp);
+    char tmp[strlen(path) + 1];
+    strcpy(tmp, path);
+    if((pat = findmatch(tmp, 0, PT_NOTFOUND)) != NULL) {
+       handle(req, fd, tmp, pat);
+    } else {
+       ch = findchild(tmp, ".notfound", &ccf);
+       if(childhandle(ch, req, fd, chinit, ccf?ccf->path:NULL))
+           childerror(req, fd);
+    }
 }
 
 static void handlefile(struct hthead *req, int fd, char *path)
 {
     struct pattern *pat;
 
-    if((pat = findmatch(path, 0, 0)) == NULL) {
+    if((pat = findmatch(path, 0, PT_FILE)) == NULL) {
        handle404(req, fd, path);
        return;
     }
            break;
        }
     }
-    if((pat = findmatch(cpath, 0, 1)) != NULL) {
+    if((pat = findmatch(cpath, 0, PT_DIR)) != NULL) {
        handle(req, fd, cpath, pat);
        goto out;
     }
     free(cpath);
 }
 
-static int checkpath(struct hthead *req, int fd, char *path, char *rest);
+static int checkpath(struct hthead *req, int fd, char *path, char *rest, int final);
 
-static int checkentry(struct hthead *req, int fd, char *path, char *rest, char *el)
+static int checkentry(struct hthead *req, int fd, char *path, char *rest, char *el, int final)
 {
     struct stat sb;
     char *newpath;
     int rv;
     
-    if(*el == '.') {
-       handle404(req, fd, sprintf3("%s/", path));
-       return(1);
-    }
+    if(*el == '.')
+       return(0);
     if(!stat(sprintf3("%s/%s", path, el), &sb)) {
        if(S_ISDIR(sb.st_mode)) {
            if(!*rest) {
                return(1);
            }
            newpath = sprintf2("%s/%s", path, el);
-           rv = checkpath(req, fd, newpath, rest + 1);
+           rv = checkpath(req, fd, newpath, rest + 1, final);
            free(newpath);
            return(rv);
        } else if(S_ISREG(sb.st_mode)) {
     return(0);
 }
 
-static int checkpath(struct hthead *req, int fd, char *path, char *rest)
+static int checkpath(struct hthead *req, int fd, char *path, char *rest, int final)
 {
     char *p, *el;
     int rv;
        goto out;
     }
     if(strchr(el, '/') || (!*el && *rest)) {
-       handle404(req, fd, sprintf3("%s/", path));
-       rv = 1;
+       rv = 0;
        goto out;
     }
     if(!*el) {
        rv = 1;
        goto out;
     }
-    rv = checkentry(req, fd, path, rest, el);
+    rv = checkentry(req, fd, path, rest, el, final);
     
 out:
+    if(final && !rv) {
+       handle404(req, fd, sprintf3("%s/", path));
+       rv = 1;
+    }
     if(el != NULL)
        free(el);
     return(rv);
 static void serve(struct hthead *req, int fd)
 {
     now = time(NULL);
-    if(!checkpath(req, fd, ".", req->rest))
-       handle404(req, fd, ".");
+    checkpath(req, fd, ".", req->rest, 1);
 }
 
 static void chldhandler(int sig)