📄 688-690.html
字号:
<HTML>
<HEAD>
<TITLE>Special Edition Using Linux, Fourth Edition:Configuring Apache</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!--ISBN=0789717468//-->
<!--TITLE=Special Edition Using Linux, Fourth Edition//-->
<!--AUTHOR=Jack Tackett//-->
<!--AUTHOR=Jr.//-->
<!--AUTHOR=Steve Burnett//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Que//-->
<!--CHAPTER=36//-->
<!--PAGES=688-690//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="686-688.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="690-693.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><TT>Redirect</TT> does just that—redirects the request to another resource. That resource could be on the same machine or somewhere else on the Net. Also, the match will be a substring match, starting from the beginning. For example, if you entered the command</P>
<!-- CODE SNIP //-->
<PRE>
Redirect /newyork http://myhost.com/maps/states/newyork
</PRE>
<!-- END CODE SNIP //-->
<P>then a request for
</P>
<!-- CODE SNIP //-->
<PRE>
<A HREF="http://myhost.com/newyork/index.html">http://myhost.com/newyork/index.html</A>
</PRE>
<!-- END CODE SNIP //-->
<P>would get redirected to
</P>
<!-- CODE SNIP //-->
<PRE>
<A HREF="http://myhost.com/maps/states/newyork/index.html">http://myhost.com/maps/states/newyork/index.html</A>
</PRE>
<!-- END CODE SNIP //-->
<P>Of course, the second argument to <TT>Redirect</TT> can be an URL at some other site. Just make sure that you know what you’re doing.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>CAUTION: </B><BR>Be wary of creating loops accidentally. For example,<!-- CODE SNIP //-->
<PRE>
Redirect /newyork http://myhost.com/newyork/newyork
</PRE>
<!-- END CODE SNIP //-->
<P>can have particularly deleterious effects on the server.<HR></FONT>
</BLOCKQUOTE>
</P>
<H4 ALIGN="LEFT"><A NAME="Heading6"></A><FONT COLOR="#000077">A Better Way to Activate CGI Scripts</FONT></H4>
<P>You read earlier that there’s a more elegant way of activating CGI scripts than by using <TT>ScriptAlias</TT>. You can use the <TT>AddType</TT> directive and create a custom MIME type, like so:</P>
<!-- CODE SNIP //-->
<PRE>
AddType application/x-httpd-cgi cgi
</PRE>
<!-- END CODE SNIP //-->
<P>When the server gets a request for a CGI file, the server maps to that MIME type and then catches itself and says, “I need to execute this rather than just dish it out like regular files.” Thus, you can have CGI files in the same directories as HTML, GIF, and all your other files.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading7"></A><FONT COLOR="#000077">Directory Indexing</FONT></H4>
<P>When Apache is given a URL to a directory instead of to a particular file as in this example:
</P>
<!-- CODE SNIP //-->
<PRE>
<A HREF="http://myhost.com/statistics/">http://myhost.com/statistics/</A>
</PRE>
<!-- END CODE SNIP //-->
<P>Apache first looks for a file specified by the <TT>DirectoryIndex</TT> directive in srm.conf. In the default configs, this file is index.html. You can set a list of files to search for or even an absolute path to a page or CGI script:</P>
<!-- CODE SNIP //-->
<PRE>
DirectoryIndex index.cgi index.html /cgi-bin/go-away
</PRE>
<!-- END CODE SNIP //-->
<P>The preceding directive says to look for an index.cgi file in the directory first. If that can’t be found, look for an index.html file in the directory. If neither can be found, redirect the request to /cgi-bin/go-away.
</P>
<P>If the Apache server fails to find a match, Apache will create—completely on-the-fly—an HTML listing of all the files available in the directory.</P>
<P>There are quite a few ways to customize the output of the directory indexing functionality. First, you need to ask yourself if you care about seeing things such as icons and last-modified times in the reports. If you do, then you want to turn to</P>
<!-- CODE SNIP //-->
<PRE>
FancyIndexing On
</PRE>
<!-- END CODE SNIP //-->
<P>Otherwise, you’ll just get a simple menu of the available files, which you may want for security or performance reasons.
</P>
<P>If you choose to use the <TT>FancyIndexing</TT> option, you must ask whether you need to customize it further and, if so, how. The default settings for the directory indexing functionality are already pretty elaborate.</P>
<P>The <TT>AddIcon</TT>, <TT>AddIconByEncoding</TT>, and <TT>AddIconByType</TT> directives customize the selection of icons next to files. <TT>AddIcon</TT> matches icons at the filename level by using the pattern</P>
<!-- CODE SNIP //-->
<PRE>
AddIcon <I>iconfile filename</I> [<I>filename</I>] [<I>filename</I>]…
</PRE>
<!-- END CODE SNIP //-->
<P>Thus, for example, the following line
</P>
<!-- CODE SNIP //-->
<PRE>
AddIcon /icons/binary.gif .bin .exe
</PRE>
<!-- END CODE SNIP //-->
<P>means that any file that ends in .bin or .exe should get the binary.gif icon attached. The filenames can also be a wildcard expression, a complete filename, or even one of two “special” names: <TT>^^DIRECTORY^^</TT> for directories and <TT>^^BLANKICON^^</TT> for blank lines. Thus, you can see lines like these:</P>
<!-- CODE SNIP //-->
<PRE>
AddIcon /icons/dir.gif ^^DIRECTORY^^
AddIcon /icons/old.gif *~
</PRE>
<!-- END CODE SNIP //-->
<P>Finally, the <TT><I>iconfile</I></TT> can be a string containing the icon file’s name and the alternate text to put into the <TT>ALT</TT> attribute. So, your examples should really be</P>
<!-- CODE SNIP //-->
<PRE>
AddIcon (BIN,/icons/binary.gif) .bin .exe
AddIcon (DIR,/icons/dir.gif) ^^DIRECTORY^^
</PRE>
<!-- END CODE SNIP //-->
<P>The <TT>AddIconByType</TT> directive is a little bit more flexible and probably comes more highly recommended in terms of actual use. Rather than tie icons to filename patterns, it ties icons to the MIME type associated with the files. The syntax is very roughly the same:</P>
<!-- CODE SNIP //-->
<PRE>
AddIconByType <I>iconfile mime-type</I> [<I>mime-type</I>]…
</PRE>
<!-- END CODE SNIP //-->
<P><I>mime-type</I> can be either the exact MIME type matching what you’ve assigned a file, or it can be a pattern match. Thus, you see entries in the default configuration files like the following:</P>
<!-- CODE SNIP //-->
<PRE>
AddIconByType (SND,/icons/sound2,gif) audio/*
</PRE>
<!-- END CODE SNIP //-->
<P>Using pattern matching is more robust than trying to match against filename suffixes only.
</P>
<P><TT>AddIconByEncoding</TT> is used mostly to distinguish compressed files from other types of files. This makes sense only if used with <TT>AddEncoding</TT> directives in your srm.conf file. The default srm.conf has these entries:</P>
<!-- CODE SNIP //-->
<PRE>
AddEncoding x-gzip gz
AddEncoding x-compress Z
AddIconByEncoding (CMP,/icons/compressed.gif) x-compress x-gzip
</PRE>
<!-- END CODE SNIP //-->
<P>The <TT>AddIconByEncoding</TT> option sets the icon next to compressed files appropriately.</P>
<P>The <TT>DefaultIcon</TT> directive specifies the icon to use when none of the patterns match a given file when the directory index is generated:</P>
<!-- CODE SNIP //-->
<PRE>
DefaultIcon /icons/unknown.gif
</PRE>
<!-- END CODE SNIP //-->
<P>It’s possible to add text to the top and the bottom of the directory index listing. This capability is very useful as it turns the directory indexing capabilities from just a UNIX-like interface into a dynamic document interface. There are two directives to control this: <TT>HeaderName</TT> and <TT>ReadmeName</TT> which specify the filenames for the content at the top and bottom of the listing, respectively. These directives are shown as follows in the default srm.conf file:</P>
<!-- CODE SNIP //-->
<PRE>
HeaderName HEADER
ReadmeName README
</PRE>
<!-- END CODE SNIP //-->
<P>When the directory index is being built, Apache will look for HEADER.html. If it finds it, it’ll throw the content into the top of the directory index. If it fails to find that file, it’ll look for just HEADER. If it finds HEADER, it will presume that the file is plain text and do things such as translate <TT><</TT> characters to the <TT>&lt;</TT> character sequence, and then insert it into the top of the directory index. The same process happens for the file README, except that the resulting text goes into the bottom of the generated directory index.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="686-688.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="690-693.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -