<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
	<channel>
		<title>Hopol's Blog</title>
		<link>http://blog.18883.com/blog/</link>
		<description>我像一顆棋子,被上帝抓在手中,我看到她的眼卻廳不到她的心.</description>
		<copyright>Copyright (C) 2004 Security Hopol All Rights Reserved.</copyright>
		<generator>Hopol's BLOG</generator>
		<lastBuildDate>Fri, 05 Dec 2008 09:50:19 +0000</lastBuildDate>
		<ttl></ttl>
		<item>
			<guid>http://blog.18883.com/blog/346</guid>
			<title>酷站推荐</title>
			<author>hopol</author>
			<description><![CDATA[<p>推荐一个好网站 ：<a href="http://www.cssclip.com">www.cssclip.com</a><br />
<br />
[attach=154]<br />
<br />
站酷(ZCOOL) 酷站推荐与素材提供: <a href="http://www.zcool.com.cn">www.zcool.com.cn</a><br />
<br />
[attach=157]<br />
<br />
肚子饿了别看：<a href="http://www.opensourcefood.com"><strong><font color="#666666">www.opensourcefood.com</font></strong></a><br />
<br />
[attach=158]<br />
<br />
cssbased：<a href="http://www.cssbased.com">www.cssbased.com</a><br />
<br />
[attach=159]<br />
<br />
URL：<a href="http://1magination.bigcartel.com/"><strong><font color="#666666">1magination.bigcartel.com</font></strong></a><br />
<br />
[attach=160]<br />
<br />
URL：<a href="http://www.onestargallery.com">www.onestargallery.com</a></p>
<p>[attach=161]<br />
<br />
URL：<a href="http://www.freelancingmoney.com">www.freelancingmoney.com</a><br />
<br />
[attach=162]<br />
<br />
URL：<a href="http://www.cmsshowcase.com"><strong><font color="#666666">www.cmsshowcase.com</font></strong></a><br />
<br />
[attach=163]<br />
<br />
URL：<a href="http://www.designbygrid.com"><strong><font color="#666666">www.designbygrid.com</font></strong></a><br />
<br />
[attach=164]<br />
<br />
URL：<a href="http://www.wallglamour.co.uk"><strong><font color="#666666">www.wallglamour.co.uk</font></strong></a><br />
<br />
[attach=165]<br />
<br />
URL：<a href="http://www.pixelmator.com"><strong><font color="#666666">www.pixelmator.com</font></strong></a><br />
<br />
[attach=166]<br />
<br />
URL：<a href="http://www.fl47.com"><strong><font color="#666666">www.fl47.com</font></strong></a><br />
<br />
[attach=167]<br />
<br />
URL：<a href="http://www.michaelchromik.de"><strong><font color="#666666">www.michaelchromik.de</font></strong></a><br />
<br />
[attach=168]<br />
<br />
URL：<a href="http://www.churchmedia.cc"><strong><font color="#666666">www.churchmedia.cc</font></strong></a><br />
<br />
[attach=169]<br />
<br />
URL：<a href="http://www.feedmyapp.com"><strong><font color="#666666">www.feedmyapp.com</font></strong></a><br />
<br />
[attach=170]<br />
<br />
URL：<a href="http://www.joomlajet.com"><strong><font color="#666666">www.joomlajet.com</font></strong></a><br />
<br />
[attach=171]<br />
<br />
URL：<a href="http://www.myvirb.net">www.myvirb.net</a><br />
<br />
[attach=172]<br />
<br />
URL：<a href="http://www.bestwebgallery.com">www.bestwebgallery.com</a><br />
<br />
[attach=173]<br />
<br />
URL：<a href="http://www.cssgalerie.net">www.cssgalerie.net</a><br />
<br />
[attach=174]</p><br /><br /><a href="http://blog.18883.com/blog/?action=show&amp;id=346" target="_blank">阅读全文</a><br /><br />]]></description>
			<link>http://blog.18883.com/blog/346</link>
			<category domain="http://blog.18883.com/blog/category/7">WEB標準</category>
			<pubDate>2007-11-21 02:18</pubDate>
		</item>
		<item>
			<guid>http://blog.18883.com/blog/274</guid>
			<title>DIV+CSS视频教程无论你是高手还是菜鸟,这个系列视频都会让你成为高手!</title>
			<author>hopol</author>
			<description><![CDATA[<p>绝对精品!!! <br />
CSS基本概念</p><br /><br /><a href="http://blog.18883.com/blog/?action=show&amp;id=274" target="_blank">阅读全文</a><br /><br />]]></description>
			<link>http://blog.18883.com/blog/274</link>
			<category domain="http://blog.18883.com/blog/category/7">WEB標準</category>
			<pubDate>2007-10-04 08:05</pubDate>
		</item>
		<item>
			<guid>http://blog.18883.com/blog/242</guid>
			<title>HTML七种加密解密</title>
			<author>hopol</author>
			<description><![CDATA[<p>&nbsp;&nbsp;&nbsp; 在做网页时（其实是网页木马呵呵），最让人烦恼的是自己辛辛苦苦写出来的客户端IE运行的JAVASCRIPT代码常常被别人轻易的拷贝，实在让自己的心里有点不是滋味，要知道自己写点东西也挺累的......^*^ <br />
　　但我们也应该清楚地认识到因为JAVASCRIPT代码是在IE中解释执行，要想绝对的保密是不可能的，我们要做的就是尽可能的增大拷贝者复制的难度，让他知难而退（但愿~!~），下面我结合自己这几年来的实践，及个人研究的心得，和大家一起来探讨一下网页中JAVASCRIPT代码的加密解密技术。</p><br /><br /><a href="http://blog.18883.com/blog/?action=show&amp;id=242" target="_blank">阅读全文</a><br /><br />]]></description>
			<link>http://blog.18883.com/blog/242</link>
			<category domain="http://blog.18883.com/blog/category/7">WEB標準</category>
			<pubDate>2007-08-23 10:57</pubDate>
		</item>
		<item>
			<guid>http://blog.18883.com/blog/239</guid>
			<title>Formset表单设置函数库</title>
			<author>hopol</author>
			<description><![CDATA[B/S开发，最基础的人机交互是什么？数据来自哪里？（产生的和输入的[废话...此处省略N万字....]）。是表单。常规的软件都必不可少的过程是&nbsp;&nbsp;用户输入--》处理--》存储--》读取修改。数据存在数据库里木用，要动起来，就要交互。于是..........<br /><br /><a href="http://blog.18883.com/blog/?action=show&amp;id=239" target="_blank">阅读全文</a><br /><br />]]></description>
			<link>http://blog.18883.com/blog/239</link>
			<category domain="http://blog.18883.com/blog/category/7">WEB標準</category>
			<pubDate>2007-08-17 14:12</pubDate>
		</item>
		<item>
			<guid>http://blog.18883.com/blog/238</guid>
			<title>动态表单的使用</title>
			<author>hopol</author>
			<description><![CDATA[<p>
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">&lt;</span><span style="color: #0000BB">script</span><span style="color: #007700">&gt;<br />function&nbsp;$(</span><span style="color: #0000BB">s</span><span style="color: #007700">){</span><span style="color: #FF8000">//返回对象<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #0000BB">document</span><span style="color: #007700">.</span><span style="color: #0000BB">getElementById</span><span style="color: #007700">(</span><span style="color: #0000BB">s</span><span style="color: #007700">);<br />}<br />function&nbsp;</span><span style="color: #0000BB">on_it</span><span style="color: #007700">(</span><span style="color: #0000BB">obj</span><span style="color: #007700">){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">obj</span><span style="color: #007700">.</span><span style="color: #0000BB">style</span><span style="color: #007700">.</span><span style="color: #0000BB">backgroundColor&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"#98AB6F"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(</span><span style="color: #DD0000">'content'</span><span style="color: #007700">).</span><span style="color: #0000BB">innerHTML</span><span style="color: #007700">=</span><span style="color: #DD0000">"鼠标移在上面"</span><span style="color: #007700">;<br />}<br />function&nbsp;</span><span style="color: #0000BB">move_it</span><span style="color: #007700">(</span><span style="color: #0000BB">obj</span><span style="color: #007700">){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">obj</span><span style="color: #007700">.</span><span style="color: #0000BB">style</span><span style="color: #007700">.</span><span style="color: #0000BB">backgroundColor&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"#ffffff"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(</span><span style="color: #DD0000">'content'</span><span style="color: #007700">).</span><span style="color: #0000BB">innerHTML</span><span style="color: #007700">=</span><span style="color: #DD0000">"鼠标从上面移走"</span><span style="color: #007700">;<br />}<br />function&nbsp;</span><span style="color: #0000BB">click_it</span><span style="color: #007700">(</span><span style="color: #0000BB">obj</span><span style="color: #007700">){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">obj</span><span style="color: #007700">.</span><span style="color: #0000BB">style</span><span style="color: #007700">.</span><span style="color: #0000BB">backgroundColor&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"red"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(</span><span style="color: #DD0000">'content'</span><span style="color: #007700">).</span><span style="color: #0000BB">innerHTML</span><span style="color: #007700">=</span><span style="color: #DD0000">"单击"</span><span style="color: #007700">;<br />}<br />function&nbsp;</span><span style="color: #0000BB">dbclick_it</span><span style="color: #007700">(</span><span style="color: #0000BB">obj</span><span style="color: #007700">){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">obj_id</span><span style="color: #007700">=</span><span style="color: #DD0000">"f"</span><span style="color: #007700">+</span><span style="color: #0000BB">obj</span><span style="color: #007700">.</span><span style="color: #0000BB">id</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">str</span><span style="color: #007700">=</span><span style="color: #DD0000">'&lt;input&nbsp;id="'&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">obj_id&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #DD0000">'"&nbsp;type="text"&nbsp;name="textfield"&nbsp;value="'&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">obj</span><span style="color: #007700">.</span><span style="color: #0000BB">innerHTML</span><span style="color: #007700">+&nbsp;</span><span style="color: #DD0000">'"&nbsp;/&gt;&lt;input&nbsp;name="b1"&nbsp;type="button"&nbsp;id="b1"&nbsp;value="修改"&nbsp;onclick=edit("'</span><span style="color: #007700">+</span><span style="color: #0000BB">obj</span><span style="color: #007700">.</span><span style="color: #0000BB">id</span><span style="color: #007700">+</span><span style="color: #DD0000">'")&nbsp;/&gt;&lt;input&nbsp;name="b2"&nbsp;type="button"&nbsp;id="b2"&nbsp;value="取消"&nbsp;onclick=del("'</span><span style="color: #007700">+</span><span style="color: #0000BB">obj</span><span style="color: #007700">.</span><span style="color: #0000BB">innerHTML</span><span style="color: #007700">+</span><span style="color: #DD0000">'","'&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">obj</span><span style="color: #007700">.</span><span style="color: #0000BB">id&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #DD0000">'")&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/&gt;'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">obj</span><span style="color: #007700">.</span><span style="color: #0000BB">innerHTML</span><span style="color: #007700">=</span><span style="color: #0000BB">str</span><span style="color: #007700">;<br />}<br />function&nbsp;</span><span style="color: #0000BB">edit</span><span style="color: #007700">(</span><span style="color: #0000BB">v</span><span style="color: #007700">){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(</span><span style="color: #0000BB">v</span><span style="color: #007700">).</span><span style="color: #0000BB">innerHTML</span><span style="color: #007700">=$(</span><span style="color: #DD0000">"f"</span><span style="color: #007700">+</span><span style="color: #0000BB">v</span><span style="color: #007700">).</span><span style="color: #0000BB">value</span><span style="color: #007700">;<br />}<br />function&nbsp;</span><span style="color: #0000BB">del</span><span style="color: #007700">(</span><span style="color: #0000BB">s</span><span style="color: #007700">,</span><span style="color: #0000BB">id</span><span style="color: #007700">){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(</span><span style="color: #0000BB">id</span><span style="color: #007700">).</span><span style="color: #0000BB">innerHTML</span><span style="color: #007700">=</span><span style="color: #0000BB">s</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">&lt;/script&gt;<br /></span>&lt;div&nbsp;id="x"&nbsp;onmouseover="on_it(this)"&nbsp;onmouseout="move_it(this)"&nbsp;onclick="click_it(this)"&nbsp;onDblClick="dbclick_it(this)"&gt;18883&lt;/div&gt;<br />&lt;div&nbsp;id="x2"&nbsp;onmouseover="on_it(this)"&nbsp;onmouseout="move_it(this)"&nbsp;onclick="click_it(this)"&nbsp;onDblClick="dbclick_it(this)"&gt;18883.com&lt;/div&gt;<br />&lt;div&nbsp;id="x3"&nbsp;onmouseover="on_it(this)"&nbsp;onmouseout="move_it(this)"&nbsp;onclick="click_it(this)"&nbsp;onDblClick="dbclick_it(this)"&gt;blog.18883.com&lt;/div&gt;<br />&lt;div&nbsp;id="content"&gt;这里是提示！&lt;/div&gt;</span>
</code></blockquote>
</p>
<p>javascript很简单，相信大家一看就能看懂的，具体思路，核心处理方式都在程序里，大家自行研究研究，写的不好，多指教了。</p>
<p>表单这样处理，用户体验很爽，根本感觉不到任何延迟，最关键的还是服务器压力很小，后台和php处理提交返回的也是很短的字符，资源占用很小。<br />其他表单的情况大致一致，就没罗列出来，有什么意见请和我交流。</p><br /><br /><a href="http://blog.18883.com/blog/?action=show&amp;id=238" target="_blank">阅读全文</a><br /><br />]]></description>
			<link>http://blog.18883.com/blog/238</link>
			<category domain="http://blog.18883.com/blog/category/7">WEB標準</category>
			<pubDate>2007-08-17 14:08</pubDate>
		</item>
		<item>
			<guid>http://blog.18883.com/blog/223</guid>
			<title>符合标准的对联广告代码</title>
			<author>hopol</author>
			<description><![CDATA[转自：blueidea<br /><br /><a href="http://blog.18883.com/blog/?action=show&amp;id=223" target="_blank">阅读全文</a><br /><br />]]></description>
			<link>http://blog.18883.com/blog/223</link>
			<category domain="http://blog.18883.com/blog/category/7">WEB標準</category>
			<pubDate>2007-02-05 14:31</pubDate>
		</item>
		<item>
			<guid>http://blog.18883.com/blog/201</guid>
			<title>CSS无所不能 【不能不看】</title>
			<author>hopol</author>
			<description><![CDATA[<p><font color="#ff0000" size="4">URL:</font> <a href="http://www.cssplay.co.uk/menu/index.html">http://www.cssplay.co.uk/menu/index.html</a></p>
<p>这些效果让人惊叹 !!!</p>]]></description>
			<link>http://blog.18883.com/blog/201</link>
			<category domain="http://blog.18883.com/blog/category/7">WEB標準</category>
			<pubDate>2006-06-23 12:48</pubDate>
		</item>
		<item>
			<guid>http://blog.18883.com/blog/200</guid>
			<title>一个CSS网站赏析网</title>
			<author>hopol</author>
			<description><![CDATA[<p><font color="#ff0000"><font size="4">URL:</font> </font><a href="http://www.easyshare.com.cn">http://www.easyshare.com.cn</a></p>
<p>共享一个CSS网站赏析网，供大家设计网站参考！</p>]]></description>
			<link>http://blog.18883.com/blog/200</link>
			<category domain="http://blog.18883.com/blog/category/7">WEB標準</category>
			<pubDate>2006-06-23 12:45</pubDate>
		</item>
		<item>
			<guid>http://blog.18883.com/blog/185</guid>
			<title>Apache的ReWrite的应用</title>
			<author>hopol</author>
			<description><![CDATA[<p>Apache的mod_rewrite是提供了强大URL操作的杀手级的模块，可以实现几乎所有你梦想的URL操作类型，其代价是你必须接受其复杂性，因为mod_rewrite的主要障碍就是初学者不容易理解和运用，即使是Apache专家有时也会发掘出mod_rewrite的新用途。 </p>
<p>换句话说：对mod_rewrite，或者是打退堂鼓永不再用，或者是喜欢它并一生受用。</p>
<p>ReWrite可以应用在以下方面或者解决以下问题： <br />URL的规划 <br />规范的URL <br />说明: <br />在有些网站服务器上，一个资源会拥有多个URL，在实际应用和发布中应该被使用的是规范的URL，其他的则是简写或者是内部使用的。无论用户在请求中使用什么形式的URL，他最终看见的都应该是规范的URL。 </p>
<p>说明: <br />在有些网站服务器上，一个资源会拥有多个URL，在实际应用和发布中应该被使用的是规范的URL，其他的则是简写或者是内部使用的。无论用户在请求中使用什么形式的URL，他最终看见的都应该是规范的URL。 </p>
<p>方案: <br />对所有的不规范的URL执行一个外部的HTTP重定向，以改变它在浏览器地址栏中的显示及其后继的请求。下例中的规则集用规范的/u/user替换/~user，并修正了/u/user所遗漏的后缀的斜杠。 </p>
<p>&nbsp;代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^/~([^/]+)/?(.*)&nbsp;/</span><span style="color: #0000BB">u</span><span style="color: #007700">/$</span><span style="color: #0000BB">1</span><span style="color: #007700">/$</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">[</span><span style="color: #0000BB">R</span><span style="color: #007700">]&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^/([</span><span style="color: #0000BB">uge</span><span style="color: #007700">])/([^/]+)$&nbsp;/$</span><span style="color: #0000BB">1</span><span style="color: #007700">/$</span><span style="color: #0000BB">2</span><span style="color: #007700">/&nbsp;[</span><span style="color: #0000BB">R</span><span style="color: #007700">]</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
<br /><br />规范的主机名 <br />说明: <br />... <br />方案: <br />代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteCond&nbsp;</span><span style="color: #007700">%&nbsp;!^</span><span style="color: #0000BB">fully</span><span style="color: #007700">.</span><span style="color: #0000BB">qualified</span><span style="color: #007700">.</span><span style="color: #0000BB">domain</span><span style="color: #007700">.</span><span style="color: #0000BB">name&nbsp;</span><span style="color: #007700">[</span><span style="color: #0000BB">NC</span><span style="color: #007700">]&nbsp;<br /></span><span style="color: #0000BB">RewriteCond&nbsp;</span><span style="color: #007700">%&nbsp;!^$&nbsp;<br /></span><span style="color: #0000BB">RewriteCond&nbsp;</span><span style="color: #007700">%&nbsp;!^</span><span style="color: #0000BB">80</span><span style="color: #007700">$&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^/(.*)&nbsp;</span><span style="color: #0000BB">http</span><span style="color: #007700">:</span><span style="color: #FF8000">//fully.qualified.domain.name:%/$1&nbsp;[L,R]&nbsp;<br /></span><span style="color: #0000BB">RewriteCond&nbsp;</span><span style="color: #007700">%&nbsp;!^</span><span style="color: #0000BB">fully</span><span style="color: #007700">.</span><span style="color: #0000BB">qualified</span><span style="color: #007700">.</span><span style="color: #0000BB">domain</span><span style="color: #007700">.</span><span style="color: #0000BB">name&nbsp;</span><span style="color: #007700">[</span><span style="color: #0000BB">NC</span><span style="color: #007700">]&nbsp;<br /></span><span style="color: #0000BB">RewriteCond&nbsp;</span><span style="color: #007700">%&nbsp;!^$&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^/(.*)&nbsp;</span><span style="color: #0000BB">http</span><span style="color: #007700">:</span><span style="color: #FF8000">//fully.qualified.domain.name/$1&nbsp;[L,R]</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
</p>
<p><br />被移动过的DocumentRoot <br />说明: <br />通常，网站服务器的DocumentRoot直接对应于URL&quot;/&quot;，但是，它常常不是处于最高一级，而可能只是众多数据池中的一个实体。比如，在Intranet站点中，有/e/www/(WWW的主页)、/e/sww/ (Intranet的主页)等等，而DocumentRoot指向了/e/www/，则必须保证此数据池中的所有内嵌的图片和其他元素对后继请求有效。 </p>
<p>方案: <br />只须重定向URL /到/e/www/即可。这个方案看起来很简单，但只是有了mod_rewrite模块的支持，它才简单，因为传统的URL Aliases机制(由mod_alias及其相关模块提供)只是作了一个前缀匹配，DocumentRoot是一个对所有URL的前缀，因而无法实现这样的重定向。而用mod_rewrite的确很简单: </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteEngine&nbsp;on&nbsp;<br />RewriteRule&nbsp;</span><span style="color: #007700">^/$&nbsp;/</span><span style="color: #0000BB">e</span><span style="color: #007700">/</span><span style="color: #0000BB">www</span><span style="color: #007700">/&nbsp;[</span><span style="color: #0000BB">R</span><span style="color: #007700">]</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
</p>
<p><br />后缀斜杠的问题 <br />说明: <br />每个网管对引用目录后缀斜杠的问题都有一本苦经，如果遗漏了，服务器会产生一个错误，因为如果请求是/~quux/foo而不是/~quux/foo/，服务器会去找一个叫foo的文件，而它是一个目录，所以就报错了。事实上，大多数情况下，它自己会试图修正这个错误，但是有时候需要你手工纠正，比如，在重写了许多CGI脚本中的复杂的URL以后。 </p>
<p>方案: <br />解决这个微妙问题的方案是让服务器自动添加后缀的斜杠。对此，必须使用一个外部的重定向，使浏览器正确地处理后继的对诸如图片的请求。如果仅仅作一个内部的重写，可能只对目录页面有效，而对内嵌有使用相对URL的图片的页面则无效，因为浏览器有请求内嵌目标的可能。比如，如果不用外部重定向，/~quux/foo/index.html页面中对image.gif的请求，其结果将是/~quux/image.gif！。 </p>
<p>所以，应该这样写： </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteEngine&nbsp;on&nbsp;<br />RewriteBase&nbsp;</span><span style="color: #007700">/~</span><span style="color: #0000BB">quux</span><span style="color: #007700">/&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^</span><span style="color: #0000BB">foo</span><span style="color: #007700">$&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">/&nbsp;[</span><span style="color: #0000BB">R</span><span style="color: #007700">]</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
</p>
<p>又懒又疯狂的做法是把这些写入其宿主目录中的顶级.htaccess中，但是须注意，如此会带来一些处理上的开销。 </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteEngine&nbsp;on&nbsp;<br />RewriteBase&nbsp;</span><span style="color: #007700">/~</span><span style="color: #0000BB">quux</span><span style="color: #007700">/&nbsp;<br /></span><span style="color: #0000BB">RewriteCond&nbsp;</span><span style="color: #007700">%&nbsp;-</span><span style="color: #0000BB">d&nbsp;<br />RewriteRule&nbsp;</span><span style="color: #007700">^(.+[^/])$&nbsp;$</span><span style="color: #0000BB">1</span><span style="color: #007700">/&nbsp;[</span><span style="color: #0000BB">R</span><span style="color: #007700">]</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
</p>
<p><br />集群网站的同类URL规划 <br />说明: <br />我们希望在一个Intranet集群网站中，对所有WWW服务器建立一个同类的一致性的URL规划，也就是，所有的URL(对单个服务器来说，是本地的依赖于此服务器的！)是独立于服务器的！我们需要的是一个具有独立于服务器的一致性规划的WWW名称空间，即，URL不需要包含正确的物理的目标服务器，而由集群本身来自动定位物理的目标主机。 </p>
<p>方案: <br />首先，目标服务器的信息来自(产生)于包含有用户、组以及实体的外部地图，其格式形如： </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">user1&nbsp;server_of_user1&nbsp;<br />user2&nbsp;server_of_user2&nbsp;<br /></span><span style="color: #007700">:&nbsp;:</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
</p>
<p><br />这些信息被存入map.xxx-to-host文件。其次，如果URL在一个服务器上无效，需要引导所有的服务器重定向URL </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">/</span><span style="color: #0000BB">u</span><span style="color: #007700">/</span><span style="color: #0000BB">user</span><span style="color: #007700">/</span><span style="color: #0000BB">anypath&nbsp;<br /></span><span style="color: #007700">/</span><span style="color: #0000BB">g</span><span style="color: #007700">/</span><span style="color: #0000BB">group</span><span style="color: #007700">/</span><span style="color: #0000BB">anypath&nbsp;<br /></span><span style="color: #007700">/</span><span style="color: #0000BB">e</span><span style="color: #007700">/</span><span style="color: #0000BB">entity</span><span style="color: #007700">/</span><span style="color: #0000BB">anypath</span>
</span>
</code></blockquote>
</p>
<p>到 </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">http</span><span style="color: #007700">:</span><span style="color: #FF8000">//physical-host/u/user/anypath&nbsp;<br /></span><span style="color: #0000BB">http</span><span style="color: #007700">:</span><span style="color: #FF8000">//physical-host/g/group/anypath&nbsp;<br /></span><span style="color: #0000BB">http</span><span style="color: #007700">:</span><span style="color: #FF8000">//physical-host/e/entity/anypath</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
</p>
<p><br />以下规则集依靠地图文件来完成这个操作(假定，如果一个用户在地图中没有对应的项，则使用server0为默认服务器)： </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteEngine&nbsp;on&nbsp;<br /><br /><br />RewriteMap&nbsp;user</span><span style="color: #007700">-</span><span style="color: #0000BB">to</span><span style="color: #007700">-</span><span style="color: #0000BB">host&nbsp;txt</span><span style="color: #007700">:/</span><span style="color: #0000BB">path</span><span style="color: #007700">/</span><span style="color: #0000BB">to</span><span style="color: #007700">/</span><span style="color: #0000BB">map</span><span style="color: #007700">.</span><span style="color: #0000BB">user</span><span style="color: #007700">-</span><span style="color: #0000BB">to</span><span style="color: #007700">-</span><span style="color: #0000BB">host&nbsp;<br />RewriteMap&nbsp;group</span><span style="color: #007700">-</span><span style="color: #0000BB">to</span><span style="color: #007700">-</span><span style="color: #0000BB">host&nbsp;txt</span><span style="color: #007700">:/</span><span style="color: #0000BB">path</span><span style="color: #007700">/</span><span style="color: #0000BB">to</span><span style="color: #007700">/</span><span style="color: #0000BB">map</span><span style="color: #007700">.</span><span style="color: #0000BB">group</span><span style="color: #007700">-</span><span style="color: #0000BB">to</span><span style="color: #007700">-</span><span style="color: #0000BB">host&nbsp;<br />RewriteMap&nbsp;entity</span><span style="color: #007700">-</span><span style="color: #0000BB">to</span><span style="color: #007700">-</span><span style="color: #0000BB">host&nbsp;txt</span><span style="color: #007700">:/</span><span style="color: #0000BB">path</span><span style="color: #007700">/</span><span style="color: #0000BB">to</span><span style="color: #007700">/</span><span style="color: #0000BB">map</span><span style="color: #007700">.</span><span style="color: #0000BB">entity</span><span style="color: #007700">-</span><span style="color: #0000BB">to</span><span style="color: #007700">-</span><span style="color: #0000BB">host&nbsp;<br /><br /><br />RewriteRule&nbsp;</span><span style="color: #007700">^/</span><span style="color: #0000BB">u</span><span style="color: #007700">/([^/]+)/?(.*)&nbsp;</span><span style="color: #0000BB">http</span><span style="color: #007700">:</span><span style="color: #FF8000">//$/u/$1/$2&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^/</span><span style="color: #0000BB">g</span><span style="color: #007700">/([^/]+)/?(.*)&nbsp;</span><span style="color: #0000BB">http</span><span style="color: #007700">:</span><span style="color: #FF8000">//$/g/$1/$2&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^/</span><span style="color: #0000BB">e</span><span style="color: #007700">/([^/]+)/?(.*)&nbsp;</span><span style="color: #0000BB">http</span><span style="color: #007700">:</span><span style="color: #FF8000">//$/e/$1/$2&nbsp;<br /><br /><br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^/([</span><span style="color: #0000BB">uge</span><span style="color: #007700">])/([^/]+)/?$&nbsp;/$</span><span style="color: #0000BB">1</span><span style="color: #007700">/$</span><span style="color: #0000BB">2</span><span style="color: #007700">/.</span><span style="color: #0000BB">www</span><span style="color: #007700">/&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^/([</span><span style="color: #0000BB">uge</span><span style="color: #007700">])/([^/]+)/([^.]+.+)&nbsp;/$</span><span style="color: #0000BB">1</span><span style="color: #007700">/$</span><span style="color: #0000BB">2</span><span style="color: #007700">/.</span><span style="color: #0000BB">www</span><span style="color: #007700">/$</span><span style="color: #0000BB">3</span>
</span>
</code></blockquote>
</p>
<p><br />移动宿主目录到不同的网站服务器 <br />说明: <br />通常，许多网管在建立一个新的网站服务器时，都会有这样的要求：重定向一个网站服务器上的所有宿主目录到另一个网站服务器。 </p>
<p>方案: <br />很简单，用mod_rewrite。在老的网站服务器上重定向所有的URL /~user/anypath到http://newserver/~user/anypath。 </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteEngine&nbsp;on&nbsp;<br />RewriteRule&nbsp;</span><span style="color: #007700">^/~(.+)&nbsp;</span><span style="color: #0000BB">http</span><span style="color: #007700">:</span><span style="color: #FF8000">//newserver/~$1&nbsp;[R,L]</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
</p>
<p><br />结构化的宿主目录 <br />说明: <br />一些拥有几千个用户的网站通常都使用结构化的宿主目录规划，即，每个宿主目录位于一个带有特定前缀比如其用户名的第一个字符的子目录下。那么，/~foo/anypath代表/home/f/foo/.www/anypath，而/~bar/anypath代表/home/b/bar/.www/anypath。 </p>
<p>方案: <br />可以使用下列规则集来扩展~以达到上述目的。 </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteEngine&nbsp;on&nbsp;<br />RewriteRule&nbsp;</span><span style="color: #007700">^/~(([</span><span style="color: #0000BB">a</span><span style="color: #007700">-</span><span style="color: #0000BB">z</span><span style="color: #007700">])[</span><span style="color: #0000BB">a</span><span style="color: #007700">-</span><span style="color: #0000BB">z0</span><span style="color: #007700">-</span><span style="color: #0000BB">9</span><span style="color: #007700">]+)(.*)&nbsp;/</span><span style="color: #0000BB">home</span><span style="color: #007700">/$</span><span style="color: #0000BB">2</span><span style="color: #007700">/$</span><span style="color: #0000BB">1</span><span style="color: #007700">/.</span><span style="color: #0000BB">www</span><span style="color: #007700">$</span><span style="color: #0000BB">3</span>
</span>
</code></blockquote>
</p>
<p><br />文件系统的重组 <br />说明: <br />这是一个不加雕琢的例子：一个大量使用针对目录的规则集以实现平滑观感，而从来不用调整数据结构的杀手级的应用。背景：net.sw从1992年开始，存放了我收集的免费的有效的Unix软件包。它是我的爱好也是我的工作，因为在学习计算机科学的同时，业余时间还做了多年的系统和网络的管理员。每周我都需要整理软件，因而建立了一个层次很深的目录结构来存放各种软件包： </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">drwxrwxr</span><span style="color: #007700">-</span><span style="color: #0000BB">x&nbsp;2&nbsp;netsw&nbsp;users&nbsp;512&nbsp;Aug&nbsp;3&nbsp;18</span><span style="color: #007700">:</span><span style="color: #0000BB">39&nbsp;Audio</span><span style="color: #007700">/&nbsp;<br /></span><span style="color: #0000BB">drwxrwxr</span><span style="color: #007700">-</span><span style="color: #0000BB">x&nbsp;2&nbsp;netsw&nbsp;users&nbsp;512&nbsp;Jul&nbsp;9&nbsp;14</span><span style="color: #007700">:</span><span style="color: #0000BB">37&nbsp;Benchmark</span><span style="color: #007700">/&nbsp;<br /></span><span style="color: #0000BB">drwxrwxr</span><span style="color: #007700">-</span><span style="color: #0000BB">x&nbsp;12&nbsp;netsw&nbsp;users&nbsp;512&nbsp;Jul&nbsp;9&nbsp;00</span><span style="color: #007700">:</span><span style="color: #0000BB">34&nbsp;Crypto</span><span style="color: #007700">/&nbsp;<br /></span><span style="color: #0000BB">drwxrwxr</span><span style="color: #007700">-</span><span style="color: #0000BB">x&nbsp;5&nbsp;netsw&nbsp;users&nbsp;512&nbsp;Jul&nbsp;9&nbsp;00</span><span style="color: #007700">:</span><span style="color: #0000BB">41&nbsp;Database</span><span style="color: #007700">/&nbsp;<br /></span><span style="color: #0000BB">drwxrwxr</span><span style="color: #007700">-</span><span style="color: #0000BB">x&nbsp;4&nbsp;netsw&nbsp;users&nbsp;512&nbsp;Jul&nbsp;30&nbsp;19</span><span style="color: #007700">:</span><span style="color: #0000BB">25&nbsp;Dicts</span><span style="color: #007700">/&nbsp;<br /></span><span style="color: #0000BB">drwxrwxr</span><span style="color: #007700">-</span><span style="color: #0000BB">x&nbsp;10&nbsp;netsw&nbsp;users&nbsp;512&nbsp;Jul&nbsp;9&nbsp;01</span><span style="color: #007700">:</span><span style="color: #0000BB">54&nbsp;Graphic</span><span style="color: #007700">/&nbsp;<br /></span><span style="color: #0000BB">drwxrwxr</span><span style="color: #007700">-</span><span style="color: #0000BB">x&nbsp;5&nbsp;netsw&nbsp;users&nbsp;512&nbsp;Jul&nbsp;9&nbsp;01</span><span style="color: #007700">:</span><span style="color: #0000BB">58&nbsp;Hackers</span><span style="color: #007700">/&nbsp;<br /></span><span style="color: #0000BB">drwxrwxr</span><span style="color: #007700">-</span><span style="color: #0000BB">x&nbsp;8&nbsp;netsw&nbsp;users&nbsp;512&nbsp;Jul&nbsp;9&nbsp;03</span><span style="color: #007700">:</span><span style="color: #0000BB">19&nbsp;InfoSys</span><span style="color: #007700">/&nbsp;<br /></span><span style="color: #0000BB">drwxrwxr</span><span style="color: #007700">-</span><span style="color: #0000BB">x&nbsp;3&nbsp;netsw&nbsp;users&nbsp;512&nbsp;Jul&nbsp;9&nbsp;03</span><span style="color: #007700">:</span><span style="color: #0000BB">21&nbsp;Math</span><span style="color: #007700">/&nbsp;<br /></span><span style="color: #0000BB">drwxrwxr</span><span style="color: #007700">-</span><span style="color: #0000BB">x&nbsp;3&nbsp;netsw&nbsp;users&nbsp;512&nbsp;Jul&nbsp;9&nbsp;03</span><span style="color: #007700">:</span><span style="color: #0000BB">24&nbsp;Misc</span><span style="color: #007700">/&nbsp;<br /></span><span style="color: #0000BB">drwxrwxr</span><span style="color: #007700">-</span><span style="color: #0000BB">x&nbsp;9&nbsp;netsw&nbsp;users&nbsp;512&nbsp;Aug&nbsp;1&nbsp;16</span><span style="color: #007700">:</span><span style="color: #0000BB">33&nbsp;Network</span><span style="color: #007700">/&nbsp;<br /></span><span style="color: #0000BB">drwxrwxr</span><span style="color: #007700">-</span><span style="color: #0000BB">x&nbsp;2&nbsp;netsw&nbsp;users&nbsp;512&nbsp;Jul&nbsp;9&nbsp;05</span><span style="color: #007700">:</span><span style="color: #0000BB">53&nbsp;Office</span><span style="color: #007700">/&nbsp;<br /></span><span style="color: #0000BB">drwxrwxr</span><span style="color: #007700">-</span><span style="color: #0000BB">x&nbsp;7&nbsp;netsw&nbsp;users&nbsp;512&nbsp;Jul&nbsp;9&nbsp;09</span><span style="color: #007700">:</span><span style="color: #0000BB">24&nbsp;SoftEng</span><span style="color: #007700">/&nbsp;<br /></span><span style="color: #0000BB">drwxrwxr</span><span style="color: #007700">-</span><span style="color: #0000BB">x&nbsp;7&nbsp;netsw&nbsp;users&nbsp;512&nbsp;Jul&nbsp;9&nbsp;12</span><span style="color: #007700">:</span><span style="color: #0000BB">17&nbsp;System</span><span style="color: #007700">/&nbsp;<br /></span><span style="color: #0000BB">drwxrwxr</span><span style="color: #007700">-</span><span style="color: #0000BB">x&nbsp;12&nbsp;netsw&nbsp;users&nbsp;512&nbsp;Aug&nbsp;3&nbsp;20</span><span style="color: #007700">:</span><span style="color: #0000BB">15&nbsp;Typesetting</span><span style="color: #007700">/&nbsp;<br /></span><span style="color: #0000BB">drwxrwxr</span><span style="color: #007700">-</span><span style="color: #0000BB">x&nbsp;10&nbsp;netsw&nbsp;users&nbsp;512&nbsp;Jul&nbsp;9&nbsp;14</span><span style="color: #007700">:</span><span style="color: #0000BB">08&nbsp;X11</span><span style="color: #007700">/</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
</p>
<p>1996年7月，我决定通过一个漂亮的Web接口公开我的收藏。&ldquo;漂亮&rdquo;是指提供一个接口以直接浏览整个目录结构，同时不对这个结构做任何改变 - 甚至也不在结构顶部放置CGI脚本。为什么呢？因为这个结构还要能够被FTP访问，而且我不希望其中有任何Web或者CGI的成分。<br />方案: <br />这个方案分为两个部分：第一个部分，是用于在空闲时间建立所有目录页面的CGI脚本集。我把它们放在/e/netsw/.www/，如下： </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">-</span><span style="color: #0000BB">rw</span><span style="color: #007700">-</span><span style="color: #0000BB">r</span><span style="color: #007700">--</span><span style="color: #0000BB">r</span><span style="color: #007700">--&nbsp;</span><span style="color: #0000BB">1&nbsp;netsw&nbsp;users&nbsp;1318&nbsp;Aug&nbsp;1&nbsp;18</span><span style="color: #007700">:</span><span style="color: #0000BB">10&nbsp;</span><span style="color: #007700">.</span><span style="color: #0000BB">wwwacl&nbsp;<br />drwxr</span><span style="color: #007700">-</span><span style="color: #0000BB">xr</span><span style="color: #007700">-</span><span style="color: #0000BB">x&nbsp;18&nbsp;netsw&nbsp;users&nbsp;512&nbsp;Aug&nbsp;5&nbsp;15</span><span style="color: #007700">:</span><span style="color: #0000BB">51&nbsp;DATA</span><span style="color: #007700">/&nbsp;<br />-</span><span style="color: #0000BB">rw</span><span style="color: #007700">-</span><span style="color: #0000BB">rw</span><span style="color: #007700">-</span><span style="color: #0000BB">rw</span><span style="color: #007700">-&nbsp;</span><span style="color: #0000BB">1&nbsp;netsw&nbsp;users&nbsp;372982&nbsp;Aug&nbsp;5&nbsp;16</span><span style="color: #007700">:</span><span style="color: #0000BB">35&nbsp;LOGFILE&nbsp;<br /></span><span style="color: #007700">-</span><span style="color: #0000BB">rw</span><span style="color: #007700">-</span><span style="color: #0000BB">r</span><span style="color: #007700">--</span><span style="color: #0000BB">r</span><span style="color: #007700">--&nbsp;</span><span style="color: #0000BB">1&nbsp;netsw&nbsp;users&nbsp;659&nbsp;Aug&nbsp;4&nbsp;09</span><span style="color: #007700">:</span><span style="color: #0000BB">27&nbsp;TODO&nbsp;<br /></span><span style="color: #007700">-</span><span style="color: #0000BB">rw</span><span style="color: #007700">-</span><span style="color: #0000BB">r</span><span style="color: #007700">--</span><span style="color: #0000BB">r</span><span style="color: #007700">--&nbsp;</span><span style="color: #0000BB">1&nbsp;netsw&nbsp;users&nbsp;5697&nbsp;Aug&nbsp;1&nbsp;18</span><span style="color: #007700">:</span><span style="color: #0000BB">01&nbsp;netsw</span><span style="color: #007700">-</span><span style="color: #0000BB">about</span><span style="color: #007700">.</span><span style="color: #0000BB">html&nbsp;<br /></span><span style="color: #007700">-</span><span style="color: #0000BB">rwxr</span><span style="color: #007700">-</span><span style="color: #0000BB">xr</span><span style="color: #007700">-</span><span style="color: #0000BB">x&nbsp;1&nbsp;netsw&nbsp;users&nbsp;579&nbsp;Aug&nbsp;2&nbsp;10</span><span style="color: #007700">:</span><span style="color: #0000BB">33&nbsp;netsw</span><span style="color: #007700">-</span><span style="color: #0000BB">access</span><span style="color: #007700">.</span><span style="color: #0000BB">pl&nbsp;<br /></span><span style="color: #007700">-</span><span style="color: #0000BB">rwxr</span><span style="color: #007700">-</span><span style="color: #0000BB">xr</span><span style="color: #007700">-</span><span style="color: #0000BB">x&nbsp;1&nbsp;netsw&nbsp;users&nbsp;1532&nbsp;Aug&nbsp;1&nbsp;17</span><span style="color: #007700">:</span><span style="color: #0000BB">35&nbsp;netsw</span><span style="color: #007700">-</span><span style="color: #0000BB">changes</span><span style="color: #007700">.</span><span style="color: #0000BB">cgi&nbsp;<br /></span><span style="color: #007700">-</span><span style="color: #0000BB">rwxr</span><span style="color: #007700">-</span><span style="color: #0000BB">xr</span><span style="color: #007700">-</span><span style="color: #0000BB">x&nbsp;1&nbsp;netsw&nbsp;users&nbsp;2866&nbsp;Aug&nbsp;5&nbsp;14</span><span style="color: #007700">:</span><span style="color: #0000BB">49&nbsp;netsw</span><span style="color: #007700">-</span><span style="color: #0000BB">home</span><span style="color: #007700">.</span><span style="color: #0000BB">cgi&nbsp;<br />drwxr</span><span style="color: #007700">-</span><span style="color: #0000BB">xr</span><span style="color: #007700">-</span><span style="color: #0000BB">x&nbsp;2&nbsp;netsw&nbsp;users&nbsp;512&nbsp;Jul&nbsp;8&nbsp;23</span><span style="color: #007700">:</span><span style="color: #0000BB">47&nbsp;netsw</span><span style="color: #007700">-</span><span style="color: #0000BB">img</span><span style="color: #007700">/&nbsp;<br />-</span><span style="color: #0000BB">rwxr</span><span style="color: #007700">-</span><span style="color: #0000BB">xr</span><span style="color: #007700">-</span><span style="color: #0000BB">x&nbsp;1&nbsp;netsw&nbsp;users&nbsp;24050&nbsp;Aug&nbsp;5&nbsp;15</span><span style="color: #007700">:</span><span style="color: #0000BB">49&nbsp;netsw</span><span style="color: #007700">-</span><span style="color: #0000BB">lsdir</span><span style="color: #007700">.</span><span style="color: #0000BB">cgi&nbsp;<br /></span><span style="color: #007700">-</span><span style="color: #0000BB">rwxr</span><span style="color: #007700">-</span><span style="color: #0000BB">xr</span><span style="color: #007700">-</span><span style="color: #0000BB">x&nbsp;1&nbsp;netsw&nbsp;users&nbsp;1589&nbsp;Aug&nbsp;3&nbsp;18</span><span style="color: #007700">:</span><span style="color: #0000BB">43&nbsp;netsw</span><span style="color: #007700">-</span><span style="color: #0000BB">search</span><span style="color: #007700">.</span><span style="color: #0000BB">cgi&nbsp;<br /></span><span style="color: #007700">-</span><span style="color: #0000BB">rwxr</span><span style="color: #007700">-</span><span style="color: #0000BB">xr</span><span style="color: #007700">-</span><span style="color: #0000BB">x&nbsp;1&nbsp;netsw&nbsp;users&nbsp;1885&nbsp;Aug&nbsp;1&nbsp;17</span><span style="color: #007700">:</span><span style="color: #0000BB">41&nbsp;netsw</span><span style="color: #007700">-</span><span style="color: #0000BB">tree</span><span style="color: #007700">.</span><span style="color: #0000BB">cgi&nbsp;<br /></span><span style="color: #007700">-</span><span style="color: #0000BB">rw</span><span style="color: #007700">-</span><span style="color: #0000BB">r</span><span style="color: #007700">--</span><span style="color: #0000BB">r</span><span style="color: #007700">--&nbsp;</span><span style="color: #0000BB">1&nbsp;netsw&nbsp;users&nbsp;234&nbsp;Jul&nbsp;30&nbsp;16</span><span style="color: #007700">:</span><span style="color: #0000BB">35&nbsp;netsw</span><span style="color: #007700">-</span><span style="color: #0000BB">unlimit</span><span style="color: #007700">.</span><span style="color: #0000BB">lst</span>
</span>
</code></blockquote>
</p>
<p><br />其中的DATA/子目录包含了上述目录结构，即实在的net.sw，由rdist在需要的时候自动更新。第二个部分的遗留问题是：如何连接这两个结构为一个平滑观感的URL树？我希望在运行适当的CGI脚本而使用各种URL的时候，使用户感觉不到DATA/目录的存在。方案如下：首先，我把下列配置放在服务器上DocumentRoot中的针对目录的配置文件里，以重写公布的URL /net.sw/ 为内部路径 /e/netsw： </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^</span><span style="color: #0000BB">net</span><span style="color: #007700">.</span><span style="color: #0000BB">sw</span><span style="color: #007700">$&nbsp;</span><span style="color: #0000BB">net</span><span style="color: #007700">.</span><span style="color: #0000BB">sw</span><span style="color: #007700">/&nbsp;[</span><span style="color: #0000BB">R</span><span style="color: #007700">]&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^</span><span style="color: #0000BB">net</span><span style="color: #007700">.</span><span style="color: #0000BB">sw</span><span style="color: #007700">/(.*)$&nbsp;</span><span style="color: #0000BB">e</span><span style="color: #007700">/</span><span style="color: #0000BB">netsw</span><span style="color: #007700">/$</span><span style="color: #0000BB">1</span>
</span>
</code></blockquote>
</p>
<p><br />第一条规则是针对遗漏后缀斜杠的请求的！第二条规则才是真正实现功能的。接着，就是放在针对目录的配置文件/e/netsw/.www/.wwwacl中的杀手级的配置了： </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">Options&nbsp;ExecCGI&nbsp;FollowSymLinks&nbsp;Includes&nbsp;MultiViews&nbsp;<br /><br /><br />RewriteEngine&nbsp;on&nbsp;<br /><br /><br /></span><span style="color: #FF8000">#&nbsp;we&nbsp;are&nbsp;reached&nbsp;via&nbsp;/net.sw/&nbsp;prefix&nbsp;<br /></span><span style="color: #0000BB">RewriteBase&nbsp;</span><span style="color: #007700">/</span><span style="color: #0000BB">net</span><span style="color: #007700">.</span><span style="color: #0000BB">sw</span><span style="color: #007700">/&nbsp;<br /><br /><br /></span><span style="color: #FF8000">#&nbsp;first&nbsp;we&nbsp;rewrite&nbsp;the&nbsp;root&nbsp;dir&nbsp;to&nbsp;<br />#&nbsp;the&nbsp;handling&nbsp;cgi&nbsp;script&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^$&nbsp;</span><span style="color: #0000BB">netsw</span><span style="color: #007700">-</span><span style="color: #0000BB">home</span><span style="color: #007700">.</span><span style="color: #0000BB">cgi&nbsp;</span><span style="color: #007700">[</span><span style="color: #0000BB">L</span><span style="color: #007700">]&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^</span><span style="color: #0000BB">index</span><span style="color: #007700">.</span><span style="color: #0000BB">html</span><span style="color: #007700">$&nbsp;</span><span style="color: #0000BB">netsw</span><span style="color: #007700">-</span><span style="color: #0000BB">home</span><span style="color: #007700">.</span><span style="color: #0000BB">cgi&nbsp;</span><span style="color: #007700">[</span><span style="color: #0000BB">L</span><span style="color: #007700">]&nbsp;<br /><br /><br /></span><span style="color: #FF8000">#&nbsp;strip&nbsp;out&nbsp;the&nbsp;subdirs&nbsp;when&nbsp;<br />#&nbsp;the&nbsp;browser&nbsp;requests&nbsp;us&nbsp;from&nbsp;perdir&nbsp;pages&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^.+/(</span><span style="color: #0000BB">netsw</span><span style="color: #007700">-[^/]+/.+)$&nbsp;$</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">[</span><span style="color: #0000BB">L</span><span style="color: #007700">]&nbsp;<br /><br /><br /></span><span style="color: #FF8000">#&nbsp;and&nbsp;now&nbsp;break&nbsp;the&nbsp;rewriting&nbsp;for&nbsp;local&nbsp;files&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^</span><span style="color: #0000BB">netsw</span><span style="color: #007700">-</span><span style="color: #0000BB">home</span><span style="color: #007700">.</span><span style="color: #0000BB">cgi</span><span style="color: #007700">.*&nbsp;-&nbsp;[</span><span style="color: #0000BB">L</span><span style="color: #007700">]&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^</span><span style="color: #0000BB">netsw</span><span style="color: #007700">-</span><span style="color: #0000BB">changes</span><span style="color: #007700">.</span><span style="color: #0000BB">cgi</span><span style="color: #007700">.*&nbsp;-&nbsp;[</span><span style="color: #0000BB">L</span><span style="color: #007700">]&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^</span><span style="color: #0000BB">netsw</span><span style="color: #007700">-</span><span style="color: #0000BB">search</span><span style="color: #007700">.</span><span style="color: #0000BB">cgi</span><span style="color: #007700">.*&nbsp;-&nbsp;[</span><span style="color: #0000BB">L</span><span style="color: #007700">]&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^</span><span style="color: #0000BB">netsw</span><span style="color: #007700">-</span><span style="color: #0000BB">tree</span><span style="color: #007700">.</span><span style="color: #0000BB">cgi</span><span style="color: #007700">$&nbsp;-&nbsp;[</span><span style="color: #0000BB">L</span><span style="color: #007700">]&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^</span><span style="color: #0000BB">netsw</span><span style="color: #007700">-</span><span style="color: #0000BB">about</span><span style="color: #007700">.</span><span style="color: #0000BB">html</span><span style="color: #007700">$&nbsp;-&nbsp;[</span><span style="color: #0000BB">L</span><span style="color: #007700">]&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^</span><span style="color: #0000BB">netsw</span><span style="color: #007700">-</span><span style="color: #0000BB">img</span><span style="color: #007700">/.*$&nbsp;-&nbsp;[</span><span style="color: #0000BB">L</span><span style="color: #007700">]&nbsp;<br /><br /><br /></span><span style="color: #FF8000">#&nbsp;anything&nbsp;else&nbsp;is&nbsp;a&nbsp;subdir&nbsp;which&nbsp;gets&nbsp;handled&nbsp;<br />#&nbsp;by&nbsp;another&nbsp;cgi&nbsp;script&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">!^</span><span style="color: #0000BB">netsw</span><span style="color: #007700">-</span><span style="color: #0000BB">lsdir</span><span style="color: #007700">.</span><span style="color: #0000BB">cgi</span><span style="color: #007700">.*&nbsp;-&nbsp;[</span><span style="color: #0000BB">C</span><span style="color: #007700">]&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">(.*)&nbsp;</span><span style="color: #0000BB">netsw</span><span style="color: #007700">-</span><span style="color: #0000BB">lsdir</span><span style="color: #007700">.</span><span style="color: #0000BB">cgi</span><span style="color: #007700">/$</span><span style="color: #0000BB">1</span>
</span>
</code></blockquote>
</p>
<p><br />阅读提示: </p>
<p>注意前半部分中的标志L(最后)，和无对应项('-') <br />注意后半部分中的符号!(非)，和标志C (链) <br />注意最后一条规则的全匹配模式 <br />代码: <br />NCSA imagemap和Apache mod_imap <br />说明: <br />许多人都希望在从NCSA网站服务器向较现代的Apache网站服务器转移中实现平滑过渡，即希望老的NCSA imagemap程序能在Apache的较现代的mod_imap支持下正常运作。但问题在于，到处都是通过/cgi-bin/imagemap/path/to/page.map引用imagemap程序的连接，而在Apache下，应该写成/path/to/page.map。 </p>
<p><br />方案: <br />使用全局规则在空闲时间去除所有这些请求的前缀： </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteEngine&nbsp;on&nbsp;<br />RewriteRule&nbsp;</span><span style="color: #007700">^/</span><span style="color: #0000BB">cgi</span><span style="color: #007700">-</span><span style="color: #0000BB">bin</span><span style="color: #007700">/</span><span style="color: #0000BB">imagemap</span><span style="color: #007700">(.*)&nbsp;$</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">[</span><span style="color: #0000BB">PT</span><span style="color: #007700">]</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
</p>
<p><br />在多个目录中搜索页面 <br />说明: <br />有时会有必要使网站服务器在多个目录中搜索页面，对此，MultiViews或者其他技术无能为力。 </p>
<p>方案: <br />编制一个明确的规则集以搜索目录中的文件。 </p>
<p>代码:<br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteEngine&nbsp;on&nbsp;<br /><br /><br /></span><span style="color: #FF8000">#&nbsp;first&nbsp;try&nbsp;to&nbsp;find&nbsp;it&nbsp;in&nbsp;custom/...&nbsp;<br />#&nbsp;...and&nbsp;if&nbsp;found&nbsp;stop&nbsp;and&nbsp;be&nbsp;happy:&nbsp;<br /></span><span style="color: #0000BB">RewriteCond&nbsp;</span><span style="color: #007700">/</span><span style="color: #0000BB">your</span><span style="color: #007700">/</span><span style="color: #0000BB">docroot</span><span style="color: #007700">/</span><span style="color: #0000BB">dir1</span><span style="color: #007700">/%&nbsp;-</span><span style="color: #0000BB">f&nbsp;<br />RewriteRule&nbsp;</span><span style="color: #007700">^(.+)&nbsp;/</span><span style="color: #0000BB">your</span><span style="color: #007700">/</span><span style="color: #0000BB">docroot</span><span style="color: #007700">/</span><span style="color: #0000BB">dir1</span><span style="color: #007700">/$</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">[</span><span style="color: #0000BB">L</span><span style="color: #007700">]&nbsp;<br /><br /><br /></span><span style="color: #FF8000">#&nbsp;second&nbsp;try&nbsp;to&nbsp;find&nbsp;it&nbsp;in&nbsp;pub/...&nbsp;<br />#&nbsp;...and&nbsp;if&nbsp;found&nbsp;stop&nbsp;and&nbsp;be&nbsp;happy:&nbsp;<br /></span><span style="color: #0000BB">RewriteCond&nbsp;</span><span style="color: #007700">/</span><span style="color: #0000BB">your</span><span style="color: #007700">/</span><span style="color: #0000BB">docroot</span><span style="color: #007700">/</span><span style="color: #0000BB">dir2</span><span style="color: #007700">/%&nbsp;-</span><span style="color: #0000BB">f&nbsp;<br />RewriteRule&nbsp;</span><span style="color: #007700">^(.+)&nbsp;/</span><span style="color: #0000BB">your</span><span style="color: #007700">/</span><span style="color: #0000BB">docroot</span><span style="color: #007700">/</span><span style="color: #0000BB">dir2</span><span style="color: #007700">/$</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">[</span><span style="color: #0000BB">L</span><span style="color: #007700">]&nbsp;<br /><br /><br /></span><span style="color: #FF8000">#&nbsp;else&nbsp;go&nbsp;on&nbsp;for&nbsp;other&nbsp;Alias&nbsp;or&nbsp;ScriptAlias&nbsp;directives,&nbsp;<br />#&nbsp;etc.&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^(.+)&nbsp;-&nbsp;[</span><span style="color: #0000BB">PT</span><span style="color: #007700">]</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
</p>
<p><br />按照URL的片段设置环境变量 <br />说明: <br />如果希望保持请求之间的状态信息，但又不希望使用CGI来包装所有页面，而只通过分离URL中的有用信息来编码。 </p>
<p>方案: <br />可以用一个规则集来分离出状态信息，并设置环境变量以备此后用于XSSI或CGI。如此，一个/foo/S=java/bar/的URL会被解析为/foo/bar/，而环境变量STATUS则被设置为&quot;java&quot;。 </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteEngine&nbsp;on&nbsp;<br />RewriteRule&nbsp;</span><span style="color: #007700">^(.*)/</span><span style="color: #0000BB">S</span><span style="color: #007700">=([^/]+)/(.*)&nbsp;$</span><span style="color: #0000BB">1</span><span style="color: #007700">/$</span><span style="color: #0000BB">3&nbsp;</span><span style="color: #007700">[</span><span style="color: #0000BB">E</span><span style="color: #007700">=</span><span style="color: #0000BB">STATUS</span><span style="color: #007700">:$</span><span style="color: #0000BB">2</span><span style="color: #007700">]</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
</p>
<p>&nbsp;</p>
<p>虚拟用户主机 <br />说明: <br />如果需要为用户username支持一个[url]www.username.host.domain.com[/url]的主页，但不是用在此机器上建虚拟主机的方法，而是用仅在此机器上增加一个DNS记录的方法实现。 </p>
<p>方案: <br />对HTTP/1.0的请求，这是无法实现的；但是对HTTP/1.1的在HTTP头中包含有主机名的请求，可以用以下规则集来内部地重写[url]http://www.username.host.com/anypath[/url]为/home/username/anypath： </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteEngine&nbsp;on&nbsp;<br />RewriteCond&nbsp;</span><span style="color: #007700">%&nbsp;^</span><span style="color: #0000BB">www</span><span style="color: #007700">.[^.]+.</span><span style="color: #0000BB">host</span><span style="color: #007700">.</span><span style="color: #0000BB">com</span><span style="color: #007700">$&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^(.+)&nbsp;%$</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">[</span><span style="color: #0000BB">C</span><span style="color: #007700">]&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^[</span><span style="color: #0000BB">url</span><span style="color: #007700">]</span><span style="color: #0000BB">www</span><span style="color: #007700">.([/</span><span style="color: #0000BB">url</span><span style="color: #007700">][^.]+).</span><span style="color: #0000BB">host</span><span style="color: #007700">.</span><span style="color: #0000BB">com</span><span style="color: #007700">(.*)&nbsp;/</span><span style="color: #0000BB">home</span><span style="color: #007700">/$</span><span style="color: #0000BB">1</span><span style="color: #007700">$</span><span style="color: #0000BB">2</span>
</span>
</code></blockquote>
</p>
<p><br />为外来访问者重定向宿主目录 <br />说明: <br />对不是来自本地域ourdomain.com的外来访问者的请求，重定向其宿主目录URL到另一个网站服务器[url]www.somewhere.com[/url]，有时这种做法也会用在虚拟主机的上下文中。 </p>
<p>方案: <br />只须一个重写条件: </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteEngine&nbsp;on&nbsp;<br />RewriteCond&nbsp;</span><span style="color: #007700">%&nbsp;!^.+.</span><span style="color: #0000BB">ourdomain</span><span style="color: #007700">.</span><span style="color: #0000BB">com</span><span style="color: #007700">$&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^(/~.+)&nbsp;[</span><span style="color: #0000BB">url</span><span style="color: #007700">]</span><span style="color: #0000BB">http</span><span style="color: #007700">:</span><span style="color: #FF8000">//www.somewhere.com/$1[/url]&nbsp;[R,L]</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
<br /><br />重定向失败的URL到其他网站服务器 <br />说明: <br />如何重写URL以重定向对网站服务器A的失败请求到服务器B，是一个常见的问题。一般，可以用Perl写的CGI脚本通过ErrorDocument来解决，此外，还有mod_rewrite方案。但是须注意，这种方法的执行效率不如用ErrorDocument的CGI脚本! </p>
<p>方案: <br />第一种方案，有最好的性能而灵活性欠佳，出错概率小所以安全: </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteEngine&nbsp;on&nbsp;<br />RewriteCond&nbsp;</span><span style="color: #007700">/</span><span style="color: #0000BB">your</span><span style="color: #007700">/</span><span style="color: #0000BB">docroot</span><span style="color: #007700">/%&nbsp;!-</span><span style="color: #0000BB">f&nbsp;<br />RewriteRule&nbsp;</span><span style="color: #007700">^(.+)&nbsp;[</span><span style="color: #0000BB">url</span><span style="color: #007700">]</span><span style="color: #0000BB">http</span><span style="color: #007700">:</span><span style="color: #FF8000">//webserverB.dom/$1[/url]</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
</p>
<p><br />但是其问题在于，它只对位于DocumentRoot中的页面有效。虽然可以增加更多的条件(比如同时还处理宿主目录，等等)，但是还有一个更好的方法： </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteEngine&nbsp;on&nbsp;<br />RewriteCond&nbsp;</span><span style="color: #007700">%&nbsp;!-</span><span style="color: #0000BB">U&nbsp;<br />RewriteRule&nbsp;</span><span style="color: #007700">^(.+)&nbsp;[</span><span style="color: #0000BB">url</span><span style="color: #007700">]</span><span style="color: #0000BB">http</span><span style="color: #007700">:</span><span style="color: #FF8000">//webserverB.dom/$1[/url]</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
<br /><br />这种方法使用了mod_rewrite提供的&ldquo;向前参照(look-ahead)&rdquo;的功能，是一种对所有URL类型都有效而且安全的方法。但是，对网站服务器的性能会有影响，所以如果网站服务器有一个强大的CPU，那就用这个方法。而在慢速机器上，可以用第一种方法，或者用性能更好的ErrorDocument CGI脚本。 </p>
<p>扩展的重定向 <br />说明: <br />有时候，我们会需要更多的对重定向URL的(有关字符转义机制方面的)控制。通常，Apache内核中的URL转义函数uri_escape()同时还会对anchor转义，即，类似&quot;url#anchor&quot;的URL，因此，你不能用mod_rewrite对此类URL直接重定向。那么如何实现呢? </p>
<p>方案: <br />必须用NPH-CGI脚本使它自己重定向，因为对NPH(non-parseable headers [无须解析的HTTP头])不会发生转义操作。首先，在针对服务器的配置中(应该位于所有重写规则的最后)，引入一种新的URL类型xredirect:： </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^</span><span style="color: #0000BB">xredirect</span><span style="color: #007700">:(.+)&nbsp;/</span><span style="color: #0000BB">path</span><span style="color: #007700">/</span><span style="color: #0000BB">to</span><span style="color: #007700">/</span><span style="color: #0000BB">nph</span><span style="color: #007700">-</span><span style="color: #0000BB">xredirect</span><span style="color: #007700">.</span><span style="color: #0000BB">cgi</span><span style="color: #007700">/$</span><span style="color: #0000BB">1&nbsp;<br /></span><span style="color: #007700">[</span><span style="color: #0000BB">T</span><span style="color: #007700">=</span><span style="color: #0000BB">application</span><span style="color: #007700">/</span><span style="color: #0000BB">x</span><span style="color: #007700">-</span><span style="color: #0000BB">httpd</span><span style="color: #007700">-</span><span style="color: #0000BB">cgi</span><span style="color: #007700">,</span><span style="color: #0000BB">L</span><span style="color: #007700">]</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
</p>
<p><br />以强制所有带xredirect:前缀的URL被传送到如下的nph-xredirect.cgi程序： </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #FF8000">#!/path/to/perl&nbsp;<br />##&nbsp;<br />##&nbsp;nph-xredirect.cgi&nbsp;--&nbsp;NPH/CGI&nbsp;script&nbsp;for&nbsp;extended&nbsp;redirects&nbsp;<br />##&nbsp;Copyright&nbsp;(c)&nbsp;1997&nbsp;Ralf&nbsp;S.&nbsp;Engelschall,&nbsp;All&nbsp;Rights&nbsp;Reserved.&nbsp;<br />##&nbsp;<br /><br /><br /></span><span style="color: #007700">$|&nbsp;=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;<br /></span><span style="color: #0000BB">$url&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$ENV</span><span style="color: #007700">;&nbsp;<br /><br /><br />print&nbsp;</span><span style="color: #DD0000">"HTTP/1.0&nbsp;302&nbsp;Moved&nbsp;Temporarilyn"</span><span style="color: #007700">;&nbsp;<br />print&nbsp;</span><span style="color: #DD0000">"Server:&nbsp;$ENVn"</span><span style="color: #007700">;&nbsp;<br />print&nbsp;</span><span style="color: #DD0000">"Location:&nbsp;$urln"</span><span style="color: #007700">;&nbsp;<br />print&nbsp;</span><span style="color: #DD0000">"Content-type:&nbsp;text/htmln"</span><span style="color: #007700">;&nbsp;<br />print&nbsp;</span><span style="color: #DD0000">"n"</span><span style="color: #007700">;&nbsp;<br />print&nbsp;</span><span style="color: #DD0000">"n"</span><span style="color: #007700">;&nbsp;<br />print&nbsp;</span><span style="color: #DD0000">"n"</span><span style="color: #007700">;&nbsp;<br />print&nbsp;</span><span style="color: #DD0000">"n"</span><span style="color: #007700">;&nbsp;<br />print&nbsp;</span><span style="color: #DD0000">"n"</span><span style="color: #007700">;&nbsp;<br />print&nbsp;</span><span style="color: #DD0000">"n"</span><span style="color: #007700">;&nbsp;<br />print&nbsp;</span><span style="color: #DD0000">"<br />Moved&nbsp;Temporarily&nbsp;(EXTENDED)<br />n"</span><span style="color: #007700">;&nbsp;<br />print&nbsp;</span><span style="color: #DD0000">"The&nbsp;document&nbsp;has&nbsp;moved&nbsp;here.<br />n"</span><span style="color: #007700">;&nbsp;<br />print&nbsp;</span><span style="color: #DD0000">"n"</span><span style="color: #007700">;&nbsp;<br />print&nbsp;</span><span style="color: #DD0000">"n"</span><span style="color: #007700">;&nbsp;<br /><br /><br /></span><span style="color: #FF8000">##EOF##</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
</p>
<p><br />这是一种可以重定向所有URL类型的方法，包括不被mod_rewrite直接支持的类型。所以，还可以这样重定向news:newsgroup： </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^</span><span style="color: #0000BB">anyurl&nbsp;xredirect</span><span style="color: #007700">:</span><span style="color: #0000BB">news</span><span style="color: #007700">:</span><span style="color: #0000BB">newsgroup</span>
</span>
</code></blockquote>
</p>
<p><br />注意：无须对上述规则加[R]或[R,L]，因为xredirect:会在稍后被其特殊的传送规则扩展。 <br />文档访问的多路复用 <br />说明: <br />你知道[url]http://www.perl.com/CPAN[/url]的CPAN(Comprehensive Perl Archive Network)吗？它实现了一个重定向以提供，全世界的CPAN镜像中离访问者最近的一个FTP站点，也可以称之为FTP访问多路复用服务。CPAN是通过CGI脚本实现的，那么用mod_rewrite如何实现呢？ </p>
<p>方案: <br />首先，我们注意到mod_rewrite从3.0.0版本开始，还可以重写&quot;ftp:&quot;类型。其次，对客户端顶级域名的路径最近的求取可以用RewriteMap实现。利用链式规则集，并用顶级域名作为查找多路复用地图的键，可以这样做： </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteEngine&nbsp;on&nbsp;<br />RewriteMap&nbsp;multiplex&nbsp;txt</span><span style="color: #007700">:/</span><span style="color: #0000BB">path</span><span style="color: #007700">/</span><span style="color: #0000BB">to</span><span style="color: #007700">/</span><span style="color: #0000BB">map</span><span style="color: #007700">.</span><span style="color: #0000BB">cxan&nbsp;<br />RewriteRule&nbsp;</span><span style="color: #007700">^/</span><span style="color: #0000BB">CxAN</span><span style="color: #007700">/(.*)&nbsp;%::$</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">[</span><span style="color: #0000BB">C</span><span style="color: #007700">]&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^.+.([</span><span style="color: #0000BB">a</span><span style="color: #007700">-</span><span style="color: #0000BB">zA</span><span style="color: #007700">-</span><span style="color: #0000BB">Z</span><span style="color: #007700">]+)::(.*)$&nbsp;$$</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">[</span><span style="color: #0000BB">R</span><span style="color: #007700">,</span><span style="color: #0000BB">L</span><span style="color: #007700">]&nbsp;<br /><br /><br /></span><span style="color: #FF8000">##&nbsp;<br />##&nbsp;map.cxan&nbsp;--&nbsp;Multiplexing&nbsp;Map&nbsp;for&nbsp;CxAN&nbsp;<br />##&nbsp;<br /><br /><br /></span><span style="color: #0000BB">de&nbsp;</span><span style="color: #007700">[</span><span style="color: #0000BB">url</span><span style="color: #007700">]</span><span style="color: #0000BB">ftp</span><span style="color: #007700">:</span><span style="color: #FF8000">//ftp.cxan.de/CxAN/[/url]&nbsp;<br /></span><span style="color: #0000BB">uk&nbsp;</span><span style="color: #007700">[</span><span style="color: #0000BB">url</span><span style="color: #007700">]</span><span style="color: #0000BB">ftp</span><span style="color: #007700">:</span><span style="color: #FF8000">//ftp.cxan.uk/CxAN/[/url]&nbsp;<br /></span><span style="color: #0000BB">com&nbsp;</span><span style="color: #007700">[</span><span style="color: #0000BB">url</span><span style="color: #007700">]</span><span style="color: #0000BB">ftp</span><span style="color: #007700">:</span><span style="color: #FF8000">//ftp.cxan.com/CxAN/[/url]&nbsp;<br /></span><span style="color: #007700">:&nbsp;<br /></span><span style="color: #FF8000">##EOF##</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
</p>
<p>依赖于时间的重写 <br />说明: <br />在页面内容依时间不同而变化的场合，比如重定向特定页面，许多网管仍然采用CGI脚本的方法，如何用mod_rewrite来实现呢？ </p>
<p>方案: <br />有许多类似TIME_xxx的变量可以用在重写条件中，利用STRING和=STRING的类型比较，并加以连接，就可以实现依赖于时间的重写：</p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteEngine&nbsp;on&nbsp;<br />RewriteCond&nbsp;</span><span style="color: #007700">%%&nbsp;&gt;</span><span style="color: #0000BB">0700&nbsp;<br />RewriteCond&nbsp;</span><span style="color: #007700">%%&nbsp;&lt;</span><span style="color: #0000BB">1900&nbsp;<br />RewriteRule&nbsp;</span><span style="color: #007700">^</span><span style="color: #0000BB">foo</span><span style="color: #007700">.</span><span style="color: #0000BB">html</span><span style="color: #007700">$&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">.</span><span style="color: #0000BB">day</span><span style="color: #007700">.</span><span style="color: #0000BB">html&nbsp;<br />RewriteRule&nbsp;</span><span style="color: #007700">^</span><span style="color: #0000BB">foo</span><span style="color: #007700">.</span><span style="color: #0000BB">html</span><span style="color: #007700">$&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">.</span><span style="color: #0000BB">night</span><span style="color: #007700">.</span><span style="color: #0000BB">html</span>
</span>
</code></blockquote>
</p>
<p><br />此例使URL foo.html在07:00-19:00时指向foo.day.html，而在其余时间，则指向foo.night.html，对主页是一个不错的功能... </p>
<p>对YYYY过渡为XXXX的向前兼容 <br />说明: <br />在转变了大批.html文件为.phtml，使文档.YYYY过渡成为文档.XXXX后，如何保持URL的向前兼容(仍然虚拟地存在)？ </p>
<p>方案: <br />只须按基准文件名重写，并测试带有新的扩展名的文件是否存在，如果存在，则用新的，否则，仍然用原来的。 </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #FF8000">#&nbsp;backward&nbsp;compatibility&nbsp;ruleset&nbsp;for&nbsp;<br />#&nbsp;rewriting&nbsp;document.html&nbsp;to&nbsp;document.phtml&nbsp;<br />#&nbsp;when&nbsp;and&nbsp;only&nbsp;when&nbsp;document.phtml&nbsp;exists&nbsp;<br />#&nbsp;but&nbsp;no&nbsp;longer&nbsp;document.html&nbsp;<br /></span><span style="color: #0000BB">RewriteEngine&nbsp;on&nbsp;<br />RewriteBase&nbsp;</span><span style="color: #007700">/~</span><span style="color: #0000BB">quux</span><span style="color: #007700">/&nbsp;<br /></span><span style="color: #FF8000">#&nbsp;parse&nbsp;out&nbsp;basename,&nbsp;but&nbsp;remember&nbsp;the&nbsp;fact&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^(.*).</span><span style="color: #0000BB">html</span><span style="color: #007700">$&nbsp;$</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">[</span><span style="color: #0000BB">C</span><span style="color: #007700">,</span><span style="color: #0000BB">E</span><span style="color: #007700">=</span><span style="color: #0000BB">WasHTML</span><span style="color: #007700">:</span><span style="color: #0000BB">yes</span><span style="color: #007700">]&nbsp;<br /></span><span style="color: #FF8000">#&nbsp;rewrite&nbsp;to&nbsp;document.phtml&nbsp;if&nbsp;exists&nbsp;<br /></span><span style="color: #0000BB">RewriteCond&nbsp;</span><span style="color: #007700">%.</span><span style="color: #0000BB">phtml&nbsp;</span><span style="color: #007700">-</span><span style="color: #0000BB">f&nbsp;<br />RewriteRule&nbsp;</span><span style="color: #007700">^(.*)$&nbsp;$</span><span style="color: #0000BB">1.phtml&nbsp;</span><span style="color: #007700">[</span><span style="color: #0000BB">S</span><span style="color: #007700">=</span><span style="color: #0000BB">1</span><span style="color: #007700">]&nbsp;<br /></span><span style="color: #FF8000">#&nbsp;else&nbsp;reverse&nbsp;the&nbsp;previous&nbsp;basename&nbsp;cutout&nbsp;<br /></span><span style="color: #0000BB">RewriteCond&nbsp;</span><span style="color: #007700">%&nbsp;^</span><span style="color: #0000BB">yes</span><span style="color: #007700">$&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^(.*)$&nbsp;$</span><span style="color: #0000BB">1.html</span>
</span>
</code></blockquote>
&nbsp;</p>
<p>内容的处理 <br />新旧URL(内部的) <br />说明: <br />假定已经把文件bar.html改名为foo.html，需要对老的URL向前兼容，即让用户仍然可以使用老的URL，而感觉不到文件被改名了。 </p>
<p>方案: <br />通过以下规则内部地重写老的URL为新的： </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteEngine&nbsp;on&nbsp;<br />RewriteBase&nbsp;</span><span style="color: #007700">/~</span><span style="color: #0000BB">quux</span><span style="color: #007700">/&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^</span><span style="color: #0000BB">foo</span><span style="color: #007700">.</span><span style="color: #0000BB">html</span><span style="color: #007700">$&nbsp;</span><span style="color: #0000BB">bar</span><span style="color: #007700">.</span><span style="color: #0000BB">html</span>
</span>
</code></blockquote>
<br /><br />新旧URL(外部的) <br />说明: <br />仍然假定已经把文件bar.html改名为foo.html，需要对老的URL向前兼容，但是要让用户得到文件被改名的暗示，即，其浏览器的地址栏中显示的是新的URL。 </p>
<p>方案: <br />作一个HTTP的强制重定向以改变浏览器和用户界面上的显示： </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteEngine&nbsp;on&nbsp;<br />RewriteBase&nbsp;</span><span style="color: #007700">/~</span><span style="color: #0000BB">quux</span><span style="color: #007700">/&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^</span><span style="color: #0000BB">foo</span><span style="color: #007700">.</span><span style="color: #0000BB">html</span><span style="color: #007700">$&nbsp;</span><span style="color: #0000BB">bar</span><span style="color: #007700">.</span><span style="color: #0000BB">html&nbsp;</span><span style="color: #007700">[</span><span style="color: #0000BB">R</span><span style="color: #007700">]</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
</p>
<p><br />依赖于浏览器的内容 <br />说明: <br />至少对重要的顶级页面，有时候有必要提供依赖于浏览器的最佳的内容，即对最新的Netscape提供最大化的版本，对Lynx提供最小化的版本，而对其他的浏览器则提供一个功能一般的版本。 </p>
<p>方案: <br />对此，内容协商无能为力，因为浏览器不提供其那种形式的类型，所以只能在HTTP头&quot;User-Agent&quot;上想办法。以下规则集可以完成这个操作：如果HTTP头&quot;User-Agent&quot;以&quot;Mozilla/3&quot;开头，则页面foo.html被重写为foo.NS.html，而后重写操作终止；如果是&quot;Lynx&quot;或者版本号为1和2的&quot;Mozilla&quot;，则重写为foo.20.html；而其他所有的浏览器收到的页面则是foo.32.html： </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteCond&nbsp;</span><span style="color: #007700">%&nbsp;^</span><span style="color: #0000BB">Mozilla</span><span style="color: #007700">/</span><span style="color: #0000BB">3.</span><span style="color: #007700">*&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^</span><span style="color: #0000BB">foo</span><span style="color: #007700">.</span><span style="color: #0000BB">html</span><span style="color: #007700">$&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">.</span><span style="color: #0000BB">NS</span><span style="color: #007700">.</span><span style="color: #0000BB">html&nbsp;</span><span style="color: #007700">[</span><span style="color: #0000BB">L</span><span style="color: #007700">]&nbsp;<br /><br /><br /></span><span style="color: #0000BB">RewriteCond&nbsp;</span><span style="color: #007700">%&nbsp;^</span><span style="color: #0000BB">Lynx</span><span style="color: #007700">/.*&nbsp;[OR]&nbsp;<br /></span><span style="color: #0000BB">RewriteCond&nbsp;</span><span style="color: #007700">%&nbsp;^</span><span style="color: #0000BB">Mozilla</span><span style="color: #007700">/[</span><span style="color: #0000BB">12</span><span style="color: #007700">].*&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^</span><span style="color: #0000BB">foo</span><span style="color: #007700">.</span><span style="color: #0000BB">html</span><span style="color: #007700">$&nbsp;</span><span style="color: #0000BB">foo.20</span><span style="color: #007700">.</span><span style="color: #0000BB">html&nbsp;</span><span style="color: #007700">[</span><span style="color: #0000BB">L</span><span style="color: #007700">]&nbsp;<br /><br /><br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^</span><span style="color: #0000BB">foo</span><span style="color: #007700">.</span><span style="color: #0000BB">html</span><span style="color: #007700">$&nbsp;</span><span style="color: #0000BB">foo.32</span><span style="color: #007700">.</span><span style="color: #0000BB">html&nbsp;</span><span style="color: #007700">[</span><span style="color: #0000BB">L</span><span style="color: #007700">]</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
</p>
<p>&nbsp;</p>
<p>动态镜像 <br />说明: <br />假定，需要在我们的名称空间里加入其他远程主机的页面。对FTP服务器，可以用mirror程序以在本地机器上维持一个对远程数据的最新的拷贝；对网站服务器，可以用类似的用于HTTP的webcopy程序。但这两种技术都有一个主要的缺点：此本地拷贝必须通过这个程序的执行来更新。所以，比较好的方法是，不采用静态镜像，而采用动态镜像，即，在有数据请求时自动更新(远程主机上更新的数据)。 </p>
<p>方案: <br />为此，使用Proxy Throughput功能(flag [P])，以映射远程页面甚至整个远程网络区域到我们的名称空间： </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteEngine&nbsp;on&nbsp;<br />RewriteBase&nbsp;</span><span style="color: #007700">/~</span><span style="color: #0000BB">quux</span><span style="color: #007700">/&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^</span><span style="color: #0000BB">hotsheet</span><span style="color: #007700">/(.*)$&nbsp;</span><span style="color: #0000BB">http</span><span style="color: #007700">:</span><span style="color: #FF8000">//www.tstimpreso.com/hotsheet/$1&nbsp;[P]&nbsp;<br /><br /><br /></span><span style="color: #0000BB">RewriteEngine&nbsp;on&nbsp;<br />RewriteBase&nbsp;</span><span style="color: #007700">/~</span><span style="color: #0000BB">quux</span><span style="color: #007700">/&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^</span><span style="color: #0000BB">usa</span><span style="color: #007700">-</span><span style="color: #0000BB">news</span><span style="color: #007700">.</span><span style="color: #0000BB">html</span><span style="color: #007700">$&nbsp;</span><span style="color: #0000BB">http</span><span style="color: #007700">:</span><span style="color: #FF8000">//www.quux-corp.com/news/index.html&nbsp;[P]</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
</p>
<p><br />反向动态镜像 <br />说明: <br />... <br />方案: <br />代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteEngine&nbsp;on&nbsp;<br />RewriteCond&nbsp;</span><span style="color: #007700">/</span><span style="color: #0000BB">mirror</span><span style="color: #007700">/</span><span style="color: #0000BB">of</span><span style="color: #007700">/</span><span style="color: #0000BB">remotesite</span><span style="color: #007700">/$</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">-</span><span style="color: #0000BB">U&nbsp;<br />RewriteRule&nbsp;</span><span style="color: #007700">^</span><span style="color: #0000BB">http</span><span style="color: #007700">:</span><span style="color: #FF8000">//www.remotesite.com/(.*)$&nbsp;/mirror/of/remotesite/$1</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
</p>
<p>通过Intranet取得丢失的数据 <br />说明: <br />这是一种在受防火墙保护的(内部的)Intranet(www2.quux-corp.dom)上保存和维护实际数据，而虚拟地运行企业级(外部的)Internet网站服务器(www.quux-corp.dom)的巧妙的方法。这种方法是外部服务器在空闲时间从内部服务器取得被请求的数据。 </p>
<p>方案: <br />首先，必须确保防火墙对内部服务器的保护，并只允许此外部服务器取得数据。对包过滤(packet-filtering)防火墙，可以如下制定防火墙规则： </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">ALLOW&nbsp;Host&nbsp;www</span><span style="color: #007700">.</span><span style="color: #0000BB">quux</span><span style="color: #007700">-</span><span style="color: #0000BB">corp</span><span style="color: #007700">.</span><span style="color: #0000BB">dom&nbsp;Port&nbsp;</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">1024&nbsp;</span><span style="color: #007700">--&gt;&nbsp;</span><span style="color: #0000BB">Host&nbsp;www2</span><span style="color: #007700">.</span><span style="color: #0000BB">quux</span><span style="color: #007700">-</span><span style="color: #0000BB">corp</span><span style="color: #007700">.</span><span style="color: #0000BB">dom&nbsp;Port&nbsp;80&nbsp;<br />DENY&nbsp;Host&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">Port&nbsp;</span><span style="color: #007700">*&nbsp;--&gt;&nbsp;</span><span style="color: #0000BB">Host&nbsp;www2</span><span style="color: #007700">.</span><span style="color: #0000BB">quux</span><span style="color: #007700">-</span><span style="color: #0000BB">corp</span><span style="color: #007700">.</span><span style="color: #0000BB">dom&nbsp;Port&nbsp;80</span>
</span>
</code></blockquote>
</p>
<p><br />按你的实际配置，只要对上例稍作调整即可。接着，建立通过代理后台获取丢失数据的mod_rewrite规则： </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^/~([^/]+)/?(.*)&nbsp;/</span><span style="color: #0000BB">home</span><span style="color: #007700">/$</span><span style="color: #0000BB">1</span><span style="color: #007700">/.</span><span style="color: #0000BB">www</span><span style="color: #007700">/$</span><span style="color: #0000BB">2&nbsp;<br />RewriteCond&nbsp;</span><span style="color: #007700">%&nbsp;!-</span><span style="color: #0000BB">f&nbsp;<br />RewriteCond&nbsp;</span><span style="color: #007700">%&nbsp;!-</span><span style="color: #0000BB">d&nbsp;<br />RewriteRule&nbsp;</span><span style="color: #007700">^/</span><span style="color: #0000BB">home</span><span style="color: #007700">/([^/]+)/.</span><span style="color: #0000BB">www</span><span style="color: #007700">/?(.*)&nbsp;</span><span style="color: #0000BB">http</span><span style="color: #007700">:</span><span style="color: #FF8000">//www2.quux-corp.dom/~$1/pub/$2&nbsp;[P]</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
</p>
<p><br />负载的均衡 <br />说明: <br />如何均衡www.foo.com的负载到www[0-5].foo.com(一共是6个服务器)? </p>
<p>方案: <br />这个问题有许多可能的解决方案，在此，我们讨论通称为&ldquo;基于DNS(DNS-based)的&rdquo;方案，和特殊的使用mod_rewrite的方案: </p>
<p>DNS循环(DNS Round-Robin) <br />最简单的方法是用BIND的DNS循环特性，只要按惯例设置www[0-9].foo.com的DNS的A(地址)记录，如： </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">www0&nbsp;IN&nbsp;A&nbsp;1.2.3.1&nbsp;<br />www1&nbsp;IN&nbsp;A&nbsp;1.2.3.2&nbsp;<br />www2&nbsp;IN&nbsp;A&nbsp;1.2.3.3&nbsp;<br />www3&nbsp;IN&nbsp;A&nbsp;1.2.3.4&nbsp;<br />www4&nbsp;IN&nbsp;A&nbsp;1.2.3.5&nbsp;<br />www5&nbsp;IN&nbsp;A&nbsp;1.2.3.6</span>
</span>
</code></blockquote>
</p>
<p><br />然后，增加以下各项: </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">www&nbsp;IN&nbsp;CNAME&nbsp;www0</span><span style="color: #007700">.</span><span style="color: #0000BB">foo</span><span style="color: #007700">.</span><span style="color: #0000BB">com</span><span style="color: #007700">.&nbsp;<br /></span><span style="color: #0000BB">IN&nbsp;CNAME&nbsp;www1</span><span style="color: #007700">.</span><span style="color: #0000BB">foo</span><span style="color: #007700">.</span><span style="color: #0000BB">com</span><span style="color: #007700">.&nbsp;<br /></span><span style="color: #0000BB">IN&nbsp;CNAME&nbsp;www2</span><span style="color: #007700">.</span><span style="color: #0000BB">foo</span><span style="color: #007700">.</span><span style="color: #0000BB">com</span><span style="color: #007700">.&nbsp;<br /></span><span style="color: #0000BB">IN&nbsp;CNAME&nbsp;www3</span><span style="color: #007700">.</span><span style="color: #0000BB">foo</span><span style="color: #007700">.</span><span style="color: #0000BB">com</span><span style="color: #007700">.&nbsp;<br /></span><span style="color: #0000BB">IN&nbsp;CNAME&nbsp;www4</span><span style="color: #007700">.</span><span style="color: #0000BB">foo</span><span style="color: #007700">.</span><span style="color: #0000BB">com</span><span style="color: #007700">.&nbsp;<br /></span><span style="color: #0000BB">IN&nbsp;CNAME&nbsp;www5</span><span style="color: #007700">.</span><span style="color: #0000BB">foo</span><span style="color: #007700">.</span><span style="color: #0000BB">com</span><span style="color: #007700">.&nbsp;<br /></span><span style="color: #0000BB">IN&nbsp;CNAME&nbsp;www6</span><span style="color: #007700">.</span><span style="color: #0000BB">foo</span><span style="color: #007700">.</span><span style="color: #0000BB">com</span><span style="color: #007700">.</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
</p>
<p>注意，上述看起来似乎是错误的，但事实上，它的确是BIND中的一个预期的特性，而且也可以这样用。无论如何，现在www.foo.com已经被解析，BIND可以给出www0-www6 - 虽然每次在次序上会有轻微的置换/循环，客户端的请求可以被分散到各个服务器。可是，这并不是一个优秀的负载均衡方案，因为，DNS解析信息可以被网络中其他名称服务器缓冲，而一旦www.foo.com被解析为wwwN.foo.com，则其后继请求都将被送往www.foo.com。但是最终结果是正确的，因为请求的总量的确被分散到各个服务器了 </p>
<p>DNS 负载均衡 <br />一种成熟的基于DNS的负载均衡方法是使用http://www.stanford.edu/~schemers/docs/lbnamed/lbnamed.html的lbnamed程序，它是一个Perl 5程序，带有若干辅助工具，实现了真正的基于DNS的负载均衡。 </p>
<p>代理吞吐循环(Proxy Throughput Round-Robin) <br />这是一个使用mod_rewrite及其代理吞吐特性的方法。首先，在DNS记录中，将www0.foo.com固定为www.foo.com，如下： </p>
<p>代码: <br />www IN CNAME www0.foo.com. </p>
<p><br />其次，将www0.foo.com转换为一个专职代理服务器，即，由这个机器把所有到来的URL通过内部代理分散到另外5个服务器(www1-www5)。为此，必须建立一个规则集，对所有URL调用一个负载均衡脚本lb.pl。 </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteEngine&nbsp;on&nbsp;<br />RewriteMap&nbsp;lb&nbsp;prg</span><span style="color: #007700">:/</span><span style="color: #0000BB">path</span><span style="color: #007700">/</span><span style="color: #0000BB">to</span><span style="color: #007700">/</span><span style="color: #0000BB">lb</span><span style="color: #007700">.</span><span style="color: #0000BB">pl&nbsp;<br />RewriteRule&nbsp;</span><span style="color: #007700">^/(.+)$&nbsp;$&nbsp;[</span><span style="color: #0000BB">P</span><span style="color: #007700">,</span><span style="color: #0000BB">L</span><span style="color: #007700">]</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
</p>
<p><br />以下是lb.pl: </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #FF8000">#!/path/to/perl&nbsp;<br />##&nbsp;<br />##&nbsp;lb.pl&nbsp;--&nbsp;load&nbsp;balancing&nbsp;script&nbsp;<br />##&nbsp;<br /><br /><br /></span><span style="color: #007700">$|&nbsp;=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;<br /><br /><br /></span><span style="color: #0000BB">$name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"www"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">#&nbsp;the&nbsp;hostname&nbsp;base&nbsp;<br /></span><span style="color: #0000BB">$first&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">#&nbsp;the&nbsp;first&nbsp;server&nbsp;(not&nbsp;0&nbsp;here,&nbsp;because&nbsp;0&nbsp;is&nbsp;myself)&nbsp;<br /></span><span style="color: #0000BB">$last&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">#&nbsp;the&nbsp;last&nbsp;server&nbsp;in&nbsp;the&nbsp;round-robin&nbsp;<br /></span><span style="color: #0000BB">$domain&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"foo.dom"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">#&nbsp;the&nbsp;domainname&nbsp;<br /><br /><br /></span><span style="color: #0000BB">$cnt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;<br />while&nbsp;()&nbsp;{&nbsp;<br /></span><span style="color: #0000BB">$cnt&nbsp;</span><span style="color: #007700">=&nbsp;((</span><span style="color: #0000BB">$cnt</span><span style="color: #007700">+</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;%&nbsp;(</span><span style="color: #0000BB">$last</span><span style="color: #007700">+</span><span style="color: #0000BB">1</span><span style="color: #007700">-</span><span style="color: #0000BB">$first</span><span style="color: #007700">));&nbsp;<br /></span><span style="color: #0000BB">$server&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%s%d.%s"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$name</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$cnt</span><span style="color: #007700">+</span><span style="color: #0000BB">$first</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$domain</span><span style="color: #007700">);&nbsp;<br />print&nbsp;</span><span style="color: #DD0000">"http://$server/$_"</span><span style="color: #007700">;&nbsp;<br />}&nbsp;<br /><br /><br /></span><span style="color: #FF8000">##EOF##</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
</p>
<p>最后的说明：这样有用吗？www0.foo.com似乎也会超载呀？答案是：没错，它的确会超载，但是它超载的仅仅是简单的代理吞吐请求！所有诸如SSI、CGI、ePerl等等的处理完全是由其他机器完成的，这个才是要点。 <br />硬件/TCP循环 <br />还有一个硬件解决方案。Cisco有一个叫LocalDirector的东西，实现了TCP/IP层的负载均衡，事实上，它是一个位于网站集群前端的电路级网关。如果你有足够资金而且的确需要高性能的解决方案，那么可以用这个。 </p>
<p>反向代理 <br />说明: <br />... <br />方案: <br />代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #FF8000">##&nbsp;<br />##&nbsp;apache-rproxy.conf&nbsp;--&nbsp;Apache&nbsp;configuration&nbsp;for&nbsp;Reverse&nbsp;Proxy&nbsp;Usage&nbsp;<br />##&nbsp;<br /><br /><br />#&nbsp;server&nbsp;type&nbsp;<br /></span><span style="color: #0000BB">ServerType&nbsp;standalone&nbsp;<br />Listen&nbsp;8000&nbsp;<br />MinSpareServers&nbsp;16&nbsp;<br />StartServers&nbsp;16&nbsp;<br />MaxSpareServers&nbsp;16&nbsp;<br />MaxClients&nbsp;16&nbsp;<br />MaxRequestsPerChild&nbsp;100&nbsp;<br /><br /><br /></span><span style="color: #FF8000">#&nbsp;server&nbsp;operation&nbsp;parameters&nbsp;<br /></span><span style="color: #0000BB">KeepAlive&nbsp;on&nbsp;<br />MaxKeepAliveRequests&nbsp;100&nbsp;<br />KeepAliveTimeout&nbsp;15&nbsp;<br />Timeout&nbsp;400&nbsp;<br />IdentityCheck&nbsp;off&nbsp;<br />HostnameLookups&nbsp;off&nbsp;<br /><br /><br /></span><span style="color: #FF8000">#&nbsp;paths&nbsp;to&nbsp;runtime&nbsp;files&nbsp;<br /></span><span style="color: #0000BB">PidFile&nbsp;</span><span style="color: #007700">/</span><span style="color: #0000BB">path</span><span style="color: #007700">/</span><span style="color: #0000BB">to</span><span style="color: #007700">/</span><span style="color: #0000BB">apache</span><span style="color: #007700">-</span><span style="color: #0000BB">rproxy</span><span style="color: #007700">.</span><span style="color: #0000BB">pid&nbsp;<br />LockFile&nbsp;</span><span style="color: #007700">/</span><span style="color: #0000BB">path</span><span style="color: #007700">/</span><span style="color: #0000BB">to</span><span style="color: #007700">/</span><span style="color: #0000BB">apache</span><span style="color: #007700">-</span><span style="color: #0000BB">rproxy</span><span style="color: #007700">.</span><span style="color: #0000BB">lock&nbsp;<br />ErrorLog&nbsp;</span><span style="color: #007700">/</span><span style="color: #0000BB">path</span><span style="color: #007700">/</span><span style="color: #0000BB">to</span><span style="color: #007700">/</span><span style="color: #0000BB">apache</span><span style="color: #007700">-</span><span style="color: #0000BB">rproxy</span><span style="color: #007700">.</span><span style="color: #0000BB">elog&nbsp;<br />CustomLog&nbsp;</span><span style="color: #007700">/</span><span style="color: #0000BB">path</span><span style="color: #007700">/</span><span style="color: #0000BB">to</span><span style="color: #007700">/</span><span style="color: #0000BB">apache</span><span style="color: #007700">-</span><span style="color: #0000BB">rproxy</span><span style="color: #007700">.</span><span style="color: #0000BB">dlog&nbsp;</span><span style="color: #DD0000">"%t&nbsp;%h&nbsp;-&gt;&nbsp;%e&nbsp;URL:&nbsp;%U"&nbsp;<br /><br /><br /></span><span style="color: #FF8000">#&nbsp;unused&nbsp;paths&nbsp;<br /></span><span style="color: #0000BB">ServerRoot&nbsp;</span><span style="color: #007700">/</span><span style="color: #0000BB">tmp&nbsp;<br />DocumentRoot&nbsp;</span><span style="color: #007700">/</span><span style="color: #0000BB">tmp&nbsp;<br />CacheRoot&nbsp;</span><span style="color: #007700">/</span><span style="color: #0000BB">tmp&nbsp;<br />RewriteLog&nbsp;</span><span style="color: #007700">/</span><span style="color: #0000BB">dev</span><span style="color: #007700">/</span><span style="color: #0000BB">null&nbsp;<br />TransferLog&nbsp;</span><span style="color: #007700">/</span><span style="color: #0000BB">dev</span><span style="color: #007700">/</span><span style="color: #0000BB">null&nbsp;<br />TypesConfig&nbsp;</span><span style="color: #007700">/</span><span style="color: #0000BB">dev</span><span style="color: #007700">/</span><span style="color: #0000BB">null&nbsp;<br />AccessConfig&nbsp;</span><span style="color: #007700">/</span><span style="color: #0000BB">dev</span><span style="color: #007700">/</span><span style="color: #0000BB">null&nbsp;<br />ResourceConfig&nbsp;</span><span style="color: #007700">/</span><span style="color: #0000BB">dev</span><span style="color: #007700">/</span><span style="color: #0000BB">null&nbsp;<br /><br /><br /></span><span style="color: #FF8000">#&nbsp;speed&nbsp;up&nbsp;and&nbsp;secure&nbsp;processing&nbsp;<br /><br /><br /></span><span style="color: #0000BB">Options&nbsp;</span><span style="color: #007700">-</span><span style="color: #0000BB">FollowSymLinks&nbsp;</span><span style="color: #007700">-</span><span style="color: #0000BB">SymLinksIfOwnerMatch&nbsp;<br />AllowOverride&nbsp;None&nbsp;<br /><br /><br /><br /></span><span style="color: #FF8000">#&nbsp;the&nbsp;status&nbsp;page&nbsp;for&nbsp;monitoring&nbsp;the&nbsp;reverse&nbsp;proxy&nbsp;<br /><br /><br /></span><span style="color: #0000BB">SetHandler&nbsp;server</span><span style="color: #007700">-</span><span style="color: #0000BB">status&nbsp;<br /><br /><br /><br /></span><span style="color: #FF8000">#&nbsp;enable&nbsp;the&nbsp;URL&nbsp;rewriting&nbsp;engine&nbsp;<br /></span><span style="color: #0000BB">RewriteEngine&nbsp;on&nbsp;<br />RewriteLogLevel&nbsp;0&nbsp;<br /><br /><br /></span><span style="color: #FF8000">#&nbsp;define&nbsp;a&nbsp;rewriting&nbsp;map&nbsp;with&nbsp;value-lists&nbsp;where&nbsp;<br />#&nbsp;mod_rewrite&nbsp;randomly&nbsp;chooses&nbsp;a&nbsp;particular&nbsp;value&nbsp;<br /></span><span style="color: #0000BB">RewriteMap&nbsp;server&nbsp;rnd</span><span style="color: #007700">:/</span><span style="color: #0000BB">path</span><span style="color: #007700">/</span><span style="color: #0000BB">to</span><span style="color: #007700">/</span><span style="color: #0000BB">apache</span><span style="color: #007700">-</span><span style="color: #0000BB">rproxy</span><span style="color: #007700">.</span><span style="color: #0000BB">conf</span><span style="color: #007700">-</span><span style="color: #0000BB">servers&nbsp;<br /><br /><br /></span><span style="color: #FF8000">#&nbsp;make&nbsp;sure&nbsp;the&nbsp;status&nbsp;page&nbsp;is&nbsp;handled&nbsp;locally&nbsp;<br />#&nbsp;and&nbsp;make&nbsp;sure&nbsp;no&nbsp;one&nbsp;uses&nbsp;our&nbsp;proxy&nbsp;except&nbsp;ourself&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^/</span><span style="color: #0000BB">apache</span><span style="color: #007700">-</span><span style="color: #0000BB">rproxy</span><span style="color: #007700">-</span><span style="color: #0000BB">status</span><span style="color: #007700">.*&nbsp;-&nbsp;[</span><span style="color: #0000BB">L</span><span style="color: #007700">]&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^(</span><span style="color: #0000BB">http</span><span style="color: #007700">|</span><span style="color: #0000BB">ftp</span><span style="color: #007700">):</span><span style="color: #FF8000">//.*&nbsp;-&nbsp;[F]&nbsp;<br /><br /><br />#&nbsp;now&nbsp;choose&nbsp;the&nbsp;possible&nbsp;servers&nbsp;for&nbsp;particular&nbsp;URL&nbsp;types&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^/(.*.(</span><span style="color: #0000BB">cgi</span><span style="color: #007700">|</span><span style="color: #0000BB">shtml</span><span style="color: #007700">))$&nbsp;</span><span style="color: #0000BB">to</span><span style="color: #007700">:</span><span style="color: #FF8000">//$/$1&nbsp;[S=1]&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^/(.*)$&nbsp;</span><span style="color: #0000BB">to</span><span style="color: #007700">:</span><span style="color: #FF8000">//$/$1&nbsp;<br /><br /><br />#&nbsp;and&nbsp;delegate&nbsp;the&nbsp;generated&nbsp;URL&nbsp;by&nbsp;passing&nbsp;it&nbsp;<br />#&nbsp;through&nbsp;the&nbsp;proxy&nbsp;module&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^</span><span style="color: #0000BB">to</span><span style="color: #007700">:</span><span style="color: #FF8000">//([^/]+)/(.*)&nbsp;http://$1/$2&nbsp;[E=SERVER:$1,P,L]&nbsp;<br /><br /><br />#&nbsp;and&nbsp;make&nbsp;really&nbsp;sure&nbsp;all&nbsp;other&nbsp;stuff&nbsp;is&nbsp;forbidden&nbsp;<br />#&nbsp;when&nbsp;it&nbsp;should&nbsp;survive&nbsp;the&nbsp;above&nbsp;rules...&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">.*&nbsp;-&nbsp;[</span><span style="color: #0000BB">F</span><span style="color: #007700">]&nbsp;<br /><br /><br /></span><span style="color: #FF8000">#&nbsp;enable&nbsp;the&nbsp;Proxy&nbsp;module&nbsp;without&nbsp;caching&nbsp;<br /></span><span style="color: #0000BB">ProxyRequests&nbsp;on&nbsp;<br />NoCache&nbsp;</span><span style="color: #007700">*&nbsp;<br /><br /><br /></span><span style="color: #FF8000">#&nbsp;setup&nbsp;URL&nbsp;reverse&nbsp;mapping&nbsp;for&nbsp;redirect&nbsp;reponses&nbsp;<br /></span><span style="color: #0000BB">ProxyPassReverse&nbsp;</span><span style="color: #007700">/&nbsp;</span><span style="color: #0000BB">http</span><span style="color: #007700">:</span><span style="color: #FF8000">//www1.foo.dom/&nbsp;<br /></span><span style="color: #0000BB">ProxyPassReverse&nbsp;</span><span style="color: #007700">/&nbsp;</span><span style="color: #0000BB">http</span><span style="color: #007700">:</span><span style="color: #FF8000">//www2.foo.dom/&nbsp;<br /></span><span style="color: #0000BB">ProxyPassReverse&nbsp;</span><span style="color: #007700">/&nbsp;</span><span style="color: #0000BB">http</span><span style="color: #007700">:</span><span style="color: #FF8000">//www3.foo.dom/&nbsp;<br /></span><span style="color: #0000BB">ProxyPassReverse&nbsp;</span><span style="color: #007700">/&nbsp;</span><span style="color: #0000BB">http</span><span style="color: #007700">:</span><span style="color: #FF8000">//www4.foo.dom/&nbsp;<br /></span><span style="color: #0000BB">ProxyPassReverse&nbsp;</span><span style="color: #007700">/&nbsp;</span><span style="color: #0000BB">http</span><span style="color: #007700">:</span><span style="color: #FF8000">//www5.foo.dom/&nbsp;<br /></span><span style="color: #0000BB">ProxyPassReverse&nbsp;</span><span style="color: #007700">/&nbsp;</span><span style="color: #0000BB">http</span><span style="color: #007700">:</span><span style="color: #FF8000">//www6.foo.dom/&nbsp;<br /><br /><br />##&nbsp;<br />##&nbsp;apache-rproxy.conf-servers&nbsp;--&nbsp;Apache/mod_rewrite&nbsp;selection&nbsp;table&nbsp;<br />##&nbsp;<br /><br /><br />#&nbsp;list&nbsp;of&nbsp;backend&nbsp;servers&nbsp;which&nbsp;serve&nbsp;static&nbsp;<br />#&nbsp;pages&nbsp;(HTML&nbsp;files&nbsp;and&nbsp;Images,&nbsp;etc.)&nbsp;<br /></span><span style="color: #007700">static&nbsp;</span><span style="color: #0000BB">www1</span><span style="color: #007700">.</span><span style="color: #0000BB">foo</span><span style="color: #007700">.</span><span style="color: #0000BB">dom</span><span style="color: #007700">|</span><span style="color: #0000BB">www2</span><span style="color: #007700">.</span><span style="color: #0000BB">foo</span><span style="color: #007700">.</span><span style="color: #0000BB">dom</span><span style="color: #007700">|</span><span style="color: #0000BB">www3</span><span style="color: #007700">.</span><span style="color: #0000BB">foo</span><span style="color: #007700">.</span><span style="color: #0000BB">dom</span><span style="color: #007700">|</span><span style="color: #0000BB">www4</span><span style="color: #007700">.</span><span style="color: #0000BB">foo</span><span style="color: #007700">.</span><span style="color: #0000BB">dom&nbsp;<br /><br /><br /></span><span style="color: #FF8000">#&nbsp;list&nbsp;of&nbsp;backend&nbsp;servers&nbsp;which&nbsp;serve&nbsp;dynamically&nbsp;<br />#&nbsp;generated&nbsp;page&nbsp;(CGI&nbsp;programs&nbsp;or&nbsp;mod_perl&nbsp;scripts)&nbsp;<br /></span><span style="color: #0000BB">dynamic&nbsp;www5</span><span style="color: #007700">.</span><span style="color: #0000BB">foo</span><span style="color: #007700">.</span><span style="color: #0000BB">dom</span><span style="color: #007700">|</span><span style="color: #0000BB">www6</span><span style="color: #007700">.</span><span style="color: #0000BB">foo</span><span style="color: #007700">.</span><span style="color: #0000BB">dom</span>
</span>
</code></blockquote>
</p>
<p>新的MIME类型，新的服务 <br />说明: <br />网上有许多很技巧的CGI程序，但是用法晦涩，许多网管弃之不用。即使是Apache的MEME类型的动作处理器，也仅仅在CGI程序不需要在其输入中包含特殊URL(PATH_INFO和QUERY_STRINGS)时才很好用。首先，配置一种新的后缀为.scgi(for secure CGI)文件类型，其处理器是很常见的cgiwrap程序。问题是：如果使用同类URL规划(见上述)，而用户宿主目录中的一个文件的URL是/u/user/foo/bar.scgi，可是cgiwrap要求的URL的格式是/~user/foo/bar.scgi/，以下规则解决了这个问题： </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^/[</span><span style="color: #0000BB">uge</span><span style="color: #007700">]/([^/]+)/.</span><span style="color: #0000BB">www</span><span style="color: #007700">/(.+).</span><span style="color: #0000BB">scgi</span><span style="color: #007700">(.*)&nbsp;...&nbsp;<br />...&nbsp;/</span><span style="color: #0000BB">internal</span><span style="color: #007700">/</span><span style="color: #0000BB">cgi</span><span style="color: #007700">/</span><span style="color: #0000BB">user</span><span style="color: #007700">/</span><span style="color: #0000BB">cgiwrap</span><span style="color: #007700">/~$</span><span style="color: #0000BB">1</span><span style="color: #007700">/$</span><span style="color: #0000BB">2.scgi</span><span style="color: #007700">$</span><span style="color: #0000BB">3&nbsp;</span><span style="color: #007700">[</span><span style="color: #0000BB">NS</span><span style="color: #007700">,</span><span style="color: #0000BB">T</span><span style="color: #007700">=</span><span style="color: #0000BB">application</span><span style="color: #007700">/</span><span style="color: #0000BB">x</span><span style="color: #007700">-</span><span style="color: #0000BB">http</span><span style="color: #007700">-</span><span style="color: #0000BB">cgi</span><span style="color: #007700">]</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
</p>
<p>另外，假设需要使用其他程序：wwwlog(显示access.log中的一个URL子树)和wwwidx(对一个URL子树运行Glimpse)，则必须对这些程序提供URL区域作为其操作对象。比如，对/u/user/foo/执行swwidx程序的超链是这样的： </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">/</span><span style="color: #0000BB">internal</span><span style="color: #007700">/</span><span style="color: #0000BB">cgi</span><span style="color: #007700">/</span><span style="color: #0000BB">user</span><span style="color: #007700">/</span><span style="color: #0000BB">swwidx</span><span style="color: #007700">?</span><span style="color: #0000BB">i</span><span style="color: #007700">=/</span><span style="color: #0000BB">u</span><span style="color: #007700">/</span><span style="color: #0000BB">user</span><span style="color: #007700">/</span><span style="color: #0000BB">foo</span><span style="color: #007700">/</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
</p>
<p><br />其缺点是，必须同时硬编码超链中的区域和CGI的路径，如果重组了这个区域，就需要花费大量时间来修改各个超链。 </p>
<p>方案: <br />方案是用一个特殊的新的URL格式，自动拼装CGI参数： </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^/([</span><span style="color: #0000BB">uge</span><span style="color: #007700">])/([^/]+)(/?.*)</span><span style="color: #FF8000">/*&nbsp;/internal/cgi/user/wwwidx?i=/$1/$2$3/&nbsp;<br />RewriteRule&nbsp;^/([uge])/([^/]+)(/?.*):log&nbsp;/internal/cgi/user/wwwlog?f=/$1/$2$3</span>
</span>
</code></blockquote>
</p>
<p><br />现在，这个搜索到/u/user/foo/的超链简化成了： </p>
<p>代码: <br />HREF=&quot;*&quot; </p>
<p><br />它会被内部地自动转换为 </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">/</span><span style="color: #0000BB">internal</span><span style="color: #007700">/</span><span style="color: #0000BB">cgi</span><span style="color: #007700">/</span><span style="color: #0000BB">user</span><span style="color: #007700">/</span><span style="color: #0000BB">wwwidx</span><span style="color: #007700">?</span><span style="color: #0000BB">i</span><span style="color: #007700">=/</span><span style="color: #0000BB">u</span><span style="color: #007700">/</span><span style="color: #0000BB">user</span><span style="color: #007700">/</span><span style="color: #0000BB">foo</span><span style="color: #007700">/</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
</p>
<p><br />如此，可以为使用:log的超链，拼装出调用CGI程序的参数。 </p>
<p>从静态到动态 <br />说明: <br />如何无缝转换静态页面foo.html为动态的foo.cgi，而不为浏览器/用户所察觉。 </p>
<p>方案: <br />只须重写此URL为CGI-script，以强制为可以作为CGI-script运行的正确的MIME类型。如此，对/~quux/foo.html的请求其实会执行/~quux/foo.cgi。 </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteEngine&nbsp;on&nbsp;<br />RewriteBase&nbsp;</span><span style="color: #007700">/~</span><span style="color: #0000BB">quux</span><span style="color: #007700">/&nbsp;<br /></span><span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^</span><span style="color: #0000BB">foo</span><span style="color: #007700">.</span><span style="color: #0000BB">html</span><span style="color: #007700">$&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">.</span><span style="color: #0000BB">cgi&nbsp;</span><span style="color: #007700">[</span><span style="color: #0000BB">T</span><span style="color: #007700">=</span><span style="color: #0000BB">application</span><span style="color: #007700">/</span><span style="color: #0000BB">x</span><span style="color: #007700">-</span><span style="color: #0000BB">httpd</span><span style="color: #007700">-</span><span style="color: #0000BB">cgi</span><span style="color: #007700">]</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
</p>
<p><br />空闲时间内的内容协商 <br />说明: <br />这是一个很难解的功能：动态生成的静态页面，即，它应该作为静态页面发送(从文件系统中读出，然后直接发出去)，但是如果它丢失了，则由服务器动态生成。如此，可以静态地提供CGI生成的页面，除非有人(或者是一个cronjob)删除了这些静态页面，而且其内容可以得到更新。 </p>
<p>方案: <br />以下规则集实现这个功能： <br />代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteCond&nbsp;</span><span style="color: #007700">%&nbsp;!-</span><span style="color: #0000BB">s&nbsp;<br />RewriteRule&nbsp;</span><span style="color: #007700">^</span><span style="color: #0000BB">page</span><span style="color: #007700">.</span><span style="color: #0000BB">html</span><span style="color: #007700">$&nbsp;</span><span style="color: #0000BB">page</span><span style="color: #007700">.</span><span style="color: #0000BB">cgi&nbsp;</span><span style="color: #007700">[</span><span style="color: #0000BB">T</span><span style="color: #007700">=</span><span style="color: #0000BB">application</span><span style="color: #007700">/</span><span style="color: #0000BB">x</span><span style="color: #007700">-</span><span style="color: #0000BB">httpd</span><span style="color: #007700">-</span><span style="color: #0000BB">cgi</span><span style="color: #007700">,</span><span style="color: #0000BB">L</span><span style="color: #007700">]</span><span style="color: #0000BB"></span>
</span>
</code></blockquote>
<br /><br />这样，如果page.html不存在或者文件大小为null，则对page.html的请求会导致page.cgi的运行。其中奥妙在于，page.cgi是一个将输出写入page.html的(同时也写入STDOUT)的常规的CGI脚本，执行完毕，服务器则将page.html的内容发出。如果网管需要强制更新其内容，只须删除page.html即可(通常由一个cronjob完成)。 </p>
<p>自动更新的文档 <br />说明: <br />建立一个复杂的页面，能够在用编辑器写了一个更新的版本时自动在浏览器上得到刷新，这不是很好吗？这可能吗？ </p>
<p>方案: <br />这是可行的! 这需要综合利用MIME多成分、网站服务器的NPH和mod_rewrite的URL操控特性。首先，建立一个新的URL特性：对在文件系统中更新时需要刷新的所有URL加上:refresh。 </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB">RewriteRule&nbsp;</span><span style="color: #007700">^(/[</span><span style="color: #0000BB">uge</span><span style="color: #007700">]/[^/]+/?.*):</span><span style="color: #0000BB">refresh&nbsp;</span><span style="color: #007700">/</span><span style="color: #0000BB">internal</span><span style="color: #007700">/</span><span style="color: #0000BB">cgi</span><span style="color: #007700">/</span><span style="color: #0000BB">apache</span><span style="color: #007700">/</span><span style="color: #0000BB">nph</span><span style="color: #007700">-</span><span style="color: #0000BB">refresh</span><span style="color: #007700">?</span><span style="color: #0000BB">f</span><span style="color: #007700">=$</span><span style="color: #0000BB">1</span>
</span>
</code></blockquote>
</p>
<p><br />然后，修改URL </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">/</span><span style="color: #0000BB">u</span><span style="color: #007700">/</span><span style="color: #0000BB">foo</span><span style="color: #007700">/</span><span style="color: #0000BB">bar</span><span style="color: #007700">/</span><span style="color: #0000BB">page</span><span style="color: #007700">.</span><span style="color: #0000BB">html</span><span style="color: #007700">:</span><span style="color: #0000BB">refresh</span>
</span>
</code></blockquote>
</p>
<p><br />以内部地操控此URL </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">/</span><span style="color: #0000BB">internal</span><span style="color: #007700">/</span><span style="color: #0000BB">cgi</span><span style="color: #007700">/</span><span style="color: #0000BB">apache</span><span style="color: #007700">/</span><span style="color: #0000BB">nph</span><span style="color: #007700">-</span><span style="color: #0000BB">refresh</span><span style="color: #007700">?</span><span style="color: #0000BB">f</span><span style="color: #007700">=/</span><span style="color: #0000BB">u</span><span style="color: #007700">/</span><span style="color: #0000BB">foo</span><span style="color: #007700">/</span><span style="color: #0000BB">bar</span><span style="color: #007700">/</span><span style="color: #0000BB">page</span><span style="color: #007700">.</span><span style="color: #0000BB">html</span>
</span>
</code></blockquote>
</p>
<p><br />接着就是NPH-CGI脚本部分了。虽然，人们常说&quot;left as an exercise to the reader&quot;，我还是给出答案了。 </p>
<p>代码: <br />
<blockquote><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #FF8000">#!/sw/bin/perl&nbsp;<br />##&nbsp;<br />##&nbsp;nph-refresh&nbsp;--&nbsp;NPH/CGI&nbsp;script&nbsp;for&nbsp;auto&nbsp;refreshing&nbsp;pages&nbsp;<br />##&nbsp;Copyright&nbsp;(c)&nbsp;1997&nbsp;Ralf&nbsp;S.&nbsp;Engelschall,&nbsp;All&nbsp;Rights&nbsp;Reserved.&nbsp;<br />##&nbsp;<br /></span><span style="color: #007700">$|&nbsp;=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;<br /><br /><br /></span><span style="color: #FF8000">#&nbsp;split&nbsp;the&nbsp;QUERY_STRING&nbsp;variable&nbsp;<br /></span><span style="color: #007700">@</span><span style="color: #0000BB">pairs&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">split</span><span style="color: #007700">(/&amp;/,&nbsp;</span><span style="color: #0000BB">$ENV</span><span style="color: #007700">);&nbsp;<br />foreach&nbsp;</span><span style="color: #0000BB">$pair&nbsp;</span><span style="color: #007700">(@</span><span style="color: #0000BB">pairs</span><span style="color: #007700">)&nbsp;{&nbsp;<br />(</span><span style="color: #0000BB">$name</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">split</span><span style="color: #007700">(/=/,&nbsp;</span><span style="color: #0000BB">$pair</span><span style="color: #007700">);&nbsp;<br /></span><span style="color: #0000BB">$name&nbsp;</span><span style="color: #007700">=~&nbsp;</span><span style="color: #0000BB">tr</span><span style="color: #007700">/</span><span style="color: #0000BB">A</span><span style="color: #007700">-</span><span style="color: #0000BB">Z</span><span style="color: #007700">/</span><span style="color: #0000BB">a</span><span style="color: #007700">-</span><span style="color: #0000BB">z</span><span style="color: #007700">/;&nbsp;<br /></span><span style="color: #0000BB">$name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'QS_'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$name</span><span style="color: #007700">;&nbsp;<br /></span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">=~&nbsp;</span><span style="color: #0000BB">s</span><span style="color: #007700">/%([</span><span style="color: #0000BB">a</span><span style="color: #007700">-</span><span style="color: #0000BB">fA</span><span style="color: #007700">-</span><span style="color: #0000BB">F0</span><span style="color: #007700">-</span><span style="color: #0000BB">9</span><span style="color: #007700">][</span><span style="color: #0000BB">a</span><span style="color: #007700">-</span><span style="color: #0000BB">fA</span><span style="color: #007700">-</span><span style="color: #0000BB">F0</span><span style="color: #007700">-</span><span style="color: #0000BB">9</span><span style="color: #007700">])/</span><span style="color: #0000BB">pack</span><span style="color: #007700">(</span><span style="color: #DD0000">"C"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">hex</span><span style="color: #007700">($</span><span style="color: #0000BB">1</span><span style="color: #007700">))/</span><span style="color: #0000BB">eg</span><span style="color: #007700">;&nbsp;<br />eval&nbsp;</span><span style="color: #DD0000">"$$name&nbsp;=&nbsp;"</span><span style="color: #0000BB">$value</span><span style="color: #DD0000">""</span><span style="color: #007700">;&nbsp;<br />}&nbsp;<br /></span><span style="color: #0000BB">$QS_s&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$QS_s&nbsp;eq&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">);&nbsp;<br /></span><span style="color: #0000BB">$QS_n&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">3600&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$QS_n&nbsp;eq&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">);&nbsp;<br />if&nbsp;(</span><span style="color: #0000BB">$QS_f&nbsp;eq&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">)&nbsp;{&nbsp;<br />print&nbsp;</span><span style="color: #DD0000">"HTTP/1.0&nbsp;200&nbsp;OKn"</span><span style="color: #007700">;&nbsp;<br />print&nbsp;</span><span style="color: #DD0000">"Content-type:&nbsp;text/htmlnn"</span><span style="color: #007700">;&nbsp;<br />print&nbsp;</span><span style="color: #DD0000">"&amp;b&amp;ERROR&amp;/b&amp;:&nbsp;No&nbsp;file&nbsp;givenn"</span><span style="color: #007700">;&nbsp;<br />exit(</span><span style="color: #0000BB">0</span><span style="color: #007700">);&nbsp;<br />}&nbsp;<br />if&nbsp;(!&nbsp;-</span><span style="color: #0000BB">f&nbsp;$QS_f</span><span style="color: #007700">)&nbsp;{&nbsp;<br />print&nbsp;</span><span style="color: #DD0000">"HTTP/1.0&nbsp;200&nbsp;OKn"</span><span style="color: #007700">;&nbsp;<br />print&nbsp;</span><span style="color: #DD0000">"Content-type:&nbsp;text/htmlnn"</span><span style="color: #007700">;&nbsp;<br />print&nbsp;</span><span style="color: #DD0000">"&amp;b&amp;ERROR&amp;/b&amp;:&nbsp;File&nbsp;$QS_f&nbsp;not&nbsp;foundn"</span><span style="color: #007700">;&nbsp;<br />exit(</span><span style="color: #0000BB">0</span><span style="color: #007700">);&nbsp;<br />}&nbsp;<br /><br /><br /></span><span style="color: #0000BB">sub&nbsp;print_http_headers_multipart_begin&nbsp;</span><span style="color: #007700">{&nbsp;<br />print&nbsp;</span><span style="color: #DD0000">"HTTP/1.0&nbsp;200&nbsp;OKn"</span><span style="color: #007700">;&nbsp;<br /></span><span style="color: #0000BB">$bound&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"ThisRandomString12345"</span><span style="color: #007700">;&nbsp;<br />print&nbsp;</span><span style="color: #DD0000">"Content-type:&nbsp;multipart/x-mixed-replace;boundary=$boundn"</span><span style="color: #007700">;&nbsp;<br />&amp;</span><span style="color: #0000BB">print_http_headers_multipart_next</span><span style="color: #007700">;&nbsp;<br />}&nbsp;<br /><br /><br /></span><span style="color: #0000BB">sub&nbsp;print_http_headers_multipart_next&nbsp;</span><span style="color: #007700">{&nbsp;<br />print&nbsp;</span><span style="color: #DD0000">"n--$boundn"</span><span style="color: #007700">;&nbsp;<br />}&nbsp;<br /><br /><br /></span><span style="color: #0000BB">sub&nbsp;print_http_headers_multipart_end&nbsp;</span><span style="color: #007700">{&nbsp;<br />print&nbsp;</span><span style="color: #DD0000">"n--$bound--n"</span><span style="color: #007700">;&nbsp;<br />}&nbsp;<br /><br /><br /></span><span style="color: #0000BB">sub&nbsp;displayhtml&nbsp;</span><span style="color: #007700">{&nbsp;<br /></span><span style="color: #0000BB">local</span><span style="color: #007700">(</span><span style="color: #0000BB">$buffer</span><span style="color: #007700">)&nbsp;=&nbsp;@</span><span style="color: #0000BB">_</span><span style="color: #007700">;&nbsp;<br /></span>