Customizing automatic indexing (or turning it off)

Issue: 960513-113 Product: Commerce Server, Communications Server, Enterprise Server, FastTrack Server
Created: 05/13/96 Version: 1.x, 2.x
Last Updated: 09/18/96 OS: All
Does this article answer your question? Yes No



"Automatic indexing" refers to the list of files you see if you browse
to a directory on your web server which does not have an index file
(usually "index.html" or "home.html") in it.

You can turn this off in the 2.0 web servers from the "Document
Preferences" admin page under "Content Mgmt".  On a 1.x web server,
there is no direct way to turn this off, but you can try one of the
following:

    o  If you want to turn off automatic indexing for one specific
       directory, then just put an empty (zero-byte-long) "index.html"
       file in that directory.

    o  If you want to turn off automatic indexing for all directories,
       then change the "Service fn=index-common ..." or
       "Service fn=index-simple ..." lines in your "obj.conf" file to
       one of these:

  Service fn="send-file" method="(GET|HEAD)" type="magnus-internal/directory"

       This will cause a user to get a "Forbidden" error message in
       his browser, and there will be no error logged on the server.

  Service fn="deny-existence" method="(GET|HEAD)" type="magnus-internal/directory"

       This will cause a user to get a "Not Found" error message in
       his browser, and an error will be logged on the server.

If you want the server to do something fancy with the index page it
generates for you, such as make it display all "*.html" files but no
other file types, then you can simulate the desired behavior by creating
an "index.cgi" program that, when accessed, reads a list of the files in
the directory it's in and outputs a custom page based on them.  An
example which was written for us is included below.  Be sure to turn on
CGI for your server (via the admin pages) and add "index.cgi" to the
list of default index files for your directories (this list is editable
in the admin server and it defaults to "index.html,home.html").

Note that the 1.1 server manuals contain incorrect information about
being able to set the "Description" field in the server's automatic
"fancy indexing".  It is not possible to set the "Description" field in
the 1.1 Netscape servers.


Sample code follows:

#!/usr/local/bin/perl
# index.cgi
# by Rajiv Pant (Betul) http://rajiv.org for Netscape Communications
# Re: Netscape Technical Note 20021
# Turning off "automatic indexing" or showing only certain files


# --- user defines begin ---

$document_root = '/extra/web' ;

@exclude = # These are not displayed
(
'.zip',
'.c',
'.cgi'
) ;

%icons = # These are the icons for the displayed ones
(
'gif',  'image',
'jpg',  'image',
'jpeg', 'image',
'au',   'sound',
'bin',  'binary',
'txt',  'text',
'text', 'text'
) ;

$icon_back     ='back' ;
$icon_menu    = 'menu' ;
$icon_unknown = 'unknown' ;

# --- user defines end ---

require "ctime.pl" ;

$this_dir = `pwd` ;
$this_dir =~ s#^$document_root(.*)$#$1/# ;

&header ;
&get_files ;
&show_files ;


sub header
{
print <<EOM;
Content-type: text/html

EOM
}


sub get_files
{
opendir (THIS_DIR, ".") ;
@files = grep (!/^\.$/, readdir (THIS_DIR)) ;
close (THIS_DIR) ;
}

sub show_files
{

$long_line =
"<IMG SRC=\"/mc-icons/blank.gif\" ALT=\"     \">" .
"  Name                   Last modified     Size  Description" ;

print <<EOM;
<TITLE>Index of $this_dir</TITLE>
<h1>Index of $this_dir</h1>
<PRE>
$long_line
<HR>
EOM

foreach $file (sort @files)
  {
  $show = 1 ;
  foreach $exclude (@exclude)
    {
    if ($file =~ /$exclude$/) { $show = 0 ; }
    }
  if ($show)
    {
    $date_last_modified = (stat ($file))[9] ;
    @date_last_modified = split (" ", &ctime ($date_last_modified)) ;
    $day = $date_last_modified [2] ;
    $day = "0" . $day if (length ($day) == 1) ;
    $month = $date_last_modified [1] ;
    ($yr) = $date_last_modified [5] =~ m/(..)$/ ;
    ($time) = $date_last_modified [3] =~ m/^(\d{1,2}:\d{1,2})/ ;
    $last_modified = "$day-$month-$yr $time" ;
    $size = -s $file ;
    ($size > 1024) ? $size = int ($size / 1024) ."K"
    : ($size != 0) && ($size = "1K") ;
    $file_name_truncated = $file ;
    $file_name_truncated =~ s/^(.{21}).*/$1+/ ;
    ($ext) = $file =~ /\.([^\.]*)$/ ;
    $ext =~ tr/A-Z/a-z/ ;
    ($icon, $alt) = (defined ($icons{$ext})) ?
    ($icons{$ext}, "IMG") : ($icon_unknown, "   ") ;
    (-d $file) && ($icon = $icon_menu) ;
    if ($file eq '..')
      {
      $file_name_truncated = 'Parent Directory' ;
      $icon = $icon_back ;
      }
    $img_src = "<IMG SRC=\"/mc-icons/$icon.gif\" ALT=\"[$alt]\" BORDER=0>  " ;
    print "<a href=\"$file\" name=\"$file\">" , $img_src ;
    write
    }
  }
print "</pre>\n" ;
}


format STDOUT =
@<<<<<<<<<<<<<<<<<<<<< </a>@<<<<<<<<<<<<<<< @>>>>
$file_name_truncated, $last_modified, $size
.

# End of program listing.  Rajiv Pant betul@rajiv.org  http://rajiv.org

Does this article answer your question? Yes No




 Corporate Sales: 415/937-2555; Personal Sales: 415/937-3777; Federal Sales: 415/937-3678
If you have any questions, please visit Customer Service.
 Copyright © 1997 Netscape Communications Corporation