<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rainux's Journal &#187; Apache Archives  &laquo; Rainux&#039;s Journal</title>
	<atom:link href="http://rainux.org/tag/apache/feed/" rel="self" type="application/rss+xml" />
	<link>http://rainux.org</link>
	<description>Rubyist of Vimmer</description>
	<lastBuildDate>Sat, 23 Jul 2011 12:09:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>搞定一个 BT 的 Apache URL Rewrite</title>
		<link>http://rainux.org/%e6%90%9e%e5%ae%9a%e4%b8%80%e4%b8%aa-bt-%e7%9a%84-apache-url-rewrite</link>
		<comments>http://rainux.org/%e6%90%9e%e5%ae%9a%e4%b8%80%e4%b8%aa-bt-%e7%9a%84-apache-url-rewrite#comments</comments>
		<pubDate>Wed, 21 Mar 2007 17:21:07 +0000</pubDate>
		<dc:creator>Rainux</dc:creator>
				<category><![CDATA[Software 软件]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[mod_rewrite]]></category>
		<category><![CDATA[Software_软件]]></category>

		<guid isPermaLink="false">http://www.rainux.org/2007/03/22/148</guid>
		<description><![CDATA[需求: 将 host/item.html?type=test rewrite 成 host/?q=item&#38;type=test 其中 item 和 test 是可变的，并且 ?type=test 可有可无。 解决: &#60;IfModule mod_rewrite.c&#62; RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} ^/[^/]+\.html$ RewriteCond %{QUERY_STRING} ^(type=.*)?$ RewriteRule ^([^/]+)\.html$ /?q=$1&#38;%1 [L] &#60;/IfModule&#62; 解释: ?type=test 是不会出现在 REQUEST&#95;URI 里的，必须使用 QUERY&#95;STRING 来获得它。 若有多个 RewriteCond，只能反向引用最后一个 RewriteCond 里的 pattern 的 group。 RewriteRule 后面的 pattern 是用来匹配当前 URL 的，当前 URL [...]]]></description>
			<content:encoded><![CDATA[<p>需求:</p>

<blockquote>
  <p>将 host/item.html?type=test<br />
  rewrite 成<br />
  host/?q=item&amp;type=test<br />
  其中 item 和 test 是可变的，并且 ?type=test 可有可无。  </p>
</blockquote>

<p>解决:</p>

<blockquote>
<pre><code>&lt;IfModule mod_rewrite.c&gt;
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/[^/]+\.html$
RewriteCond %{QUERY_STRING} ^(type=.*)?$
RewriteRule ^([^/]+)\.html$ /?q=$1&amp;%1 [L]
&lt;/IfModule&gt;
</code></pre>
</blockquote>

<p>解释:</p>

<ol>
<li>?type=test 是不会出现在 REQUEST&#95;URI 里的，必须使用 QUERY&#95;STRING 来获得它。</li>
<li>若有多个 RewriteCond，只能反向引用最后一个 RewriteCond 里的 pattern 的 group。</li>
<li>RewriteRule 后面的 pattern 是用来匹配当前 URL 的，当前 URL 不是 REQUEST&#95;URI。因为(因此？)它会比 REQUEST&#95;URI 少一个前导斜杠 "/"，并且它可能已经被前面的 RewriteRule 改变过。</li>
</ol>

<p>规则 BT 吧？第一个搞得我差点以为无解，第三个规则也消耗了我大量时间。本以为不用测试就能搞定的一个小问题居然花了 40 分钟左右才完成。-_-b</p>

	标签：<a href="http://rainux.org/tag/apache/" title="Apache" rel="tag">Apache</a>, <a href="http://rainux.org/tag/mod_rewrite/" title="mod_rewrite" rel="tag">mod_rewrite</a>, <a href="http://rainux.org/tag/software_%e8%bd%af%e4%bb%b6/" title="Software_软件" rel="tag">Software_软件</a><br />
]]></content:encoded>
			<wfw:commentRss>http://rainux.org/%e6%90%9e%e5%ae%9a%e4%b8%80%e4%b8%aa-bt-%e7%9a%84-apache-url-rewrite/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

