<?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>LinuxByte &#187; mod_rewrite</title>
	<atom:link href="http://www.linuxbyte.org/tag/mod_rewrite/feed" rel="self" type="application/rss+xml" />
	<link>http://www.linuxbyte.org</link>
	<description>一个Linux Blog</description>
	<lastBuildDate>Thu, 26 Jan 2012 08:13:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Apache Url Rewrite(mod_rewrite)</title>
		<link>http://www.linuxbyte.org/apache-url-rewrite.html</link>
		<comments>http://www.linuxbyte.org/apache-url-rewrite.html#comments</comments>
		<pubDate>Fri, 14 Mar 2008 09:56:14 +0000</pubDate>
		<dc:creator>xiao H</dc:creator>
				<category><![CDATA[服务器应用]]></category>
		<category><![CDATA[精品文档]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[mod_rewrite]]></category>
		<category><![CDATA[Url Rewrite]]></category>
		<category><![CDATA[url重写]]></category>
		<category><![CDATA[url重定向]]></category>

		<guid isPermaLink="false">http://www.linuxbyte.org/apache-url-rewritemod_rewrite</guid>
		<description><![CDATA[一、为什么需要用Apache Url Rewrite(mod_rewrite) 网站的生命在于不断地进行更新和维护，根据业务发展的需求转移服务器进行维护、重新组织目录结 构、变换URL甚至改变到新的域名等情况是经常发... ]]></description>
			<content:encoded><![CDATA[<p class="post">一、为什么需要用Apache Url Rewrite(mod_rewrite)</p>
<p>网站的生命在于不断地进行更新和维护，根据业务发展的需求转移服务器进行维护、重新组织目录结<br />
构、变换URL甚至改变到新的域名等情况是经常发生的。为了让客户不会因此受到任何影响，最好的方法就是<br />
使用Apache Rewrite Rule(Apache Url Rewrite)。    <span id="more-78"></span></p>
<p>二、Apache Url Rewrite的作用范围</p>
<p> 1．使用在Apache主配置文件httpd.conf中。</p>
<p> 2．使用在httpd.conf里定义的虚拟主机配置中。</p>
<p> 3．使用在基本目录的跨越配置文件.htaccess中。</p>
<p>三、Apache Url Rewrite的应用条件</p>
<p> 当用户的Web请求最终被导向到某台Web服务器的Apache守护进程，Apache根据配置文件判断该请求是<br />
主配置还是虚拟主机，再根据用户在浏览器中请求的URL来匹配Apache Url Rewrite，并且根据实际的请求路径匹<br />
配.htaccess中的Apache Url Rewrite，最后把请求的内容传回给用户。该响应可能有2种。</p>
<p> 1．将请求内容外部重定向(Redirect)到另一个URL</p>
<p> 让浏览器再次以新的URL发出请求(R=301或者R=302，临时的或是永久的重定向)。</p>
<p> 例如，一个网站有正规的URL和别名URL，对别名URL进行重定向到正规URL，或者网站改换成了新的域<br />
名，则把旧的域名重定向到新的域名。</p>
<p> 2．由Apache内部子请求代理产生新的内容送回给客户</p>
<p> 这是Apache内部根据重写后的URL，通过代理模块请求内容并将最终内容送回给客户，客户端浏览器不<br />
必再次请求，浏览器中的URL不会被重写，但实际内容由Apache根据Apache Url Rewrite后的URL生成。</p>
<p> 例如，在公司防火墙上运行的Apache启动这种代理Apache Url Rewrite，代理对内部网段上的Web服务器的请求。</p>
<p>四、Apache Url Rewrite怎样工作</p>
<p> 我们假定在编译Apache时已经把mod_rewrite编译成模块，确信您的httpd.conf中有LoadModule<br />
rewrite_module libexec/mod_rewrite.so，并且在Addmodule中有Addmodule mod_rewrite.c，则可<br />
以使用Apache Url Rewrite。</p>
<p> 当外部请求到达Apache，Apache调用Apache Url Rewrite中的定义来重写由用户浏览器指定请求的URL，最后被重<br />
写的URL如果是重定向，则送交浏览器做再一次请求；如果是代理则把重写后的URL交给代理模块请求最终的内<br />
容(Content)，最后把内容送回给浏览器。</p>
<p>五、何时使用.htaccess中的Apache Url Rewrite定义</p>
<p> 假如您对网站内容所在的服务器没有管理员权限，或者您的网站内容放在ISP的服务器上托管，无法改写<br />
主配置文件，但是您对Web站点内容所在的目录有写权限，则可以设置自己的.htaccess文件达到同样的目<br />
的。但您需要确定主配置文件中对您的网站所在的目录定义了下面的内容，否则您的.htaccess不会工作。</p>
<p> &lt; Directory /usr/local/apache/htdocs/www.abc.com&gt; options indexes followsymLinks</p>
<p> allowoverride all</p>
<p> &lt; /Directory &gt;</p>
<p>六、应用举例</p>
<p> 假定Apache被编译安装在主机192.168.1.xx的/usr/local/apache目录下面，同时编译了重写和代<br />
理模块。</p>
<p> 1．隐藏Apache下的某个目录，使得对该目录的任何请求都重定向到另一个文件</p>
<p> (1)httpd.conf的实现方法</p>
<p> 我们将下面的部分放到/usr/local/apache/conf/httpd.conf中。</p>
<p> &lt; Directory “/usr/local/apache/htdocs/manual/”&gt; options Indexes followsymlinks</p>
<p> allowoverride all<br />
 rewriteengine on<br />
 rewritebase /<br />
 rewriterule ^(.*)$ index.html.en [R=301]</p>
<p> &lt; /Directory &gt;</p>
<p> 注: “rewriteengine on”为重写引擎开关，如果设为“off”，则任何Apache Url Rewrite定义将不被应用，<br />
该开关的另一用处就是如果为了临时去掉Apache Url Rewrite，可以将引擎开关设为“off”再重新启动Apache即可，不<br />
必将其中的各条Apache Url Rewrite注释掉。</p>
<p> “rewritebase /”的作用是如果在下面的rewriterule定义中被重写后的部分(此处为文件名<br />
index.html.en)前面没有“/”，则表明是相对目录，相对于这个rewritebase后面的定义也就是/usr/<br />
local/apache/htdocs/index.html.en，否则，如果此处没有“rewritebase /”这一项，则被重写成</p>
<p>http://192.168.1.xx/usr/local/apache/htdocs/manual/index.html.en，显然是不正确的。</p>
<p> 我们也可以不用“rewritebase /”，而是将其改为如下部分。</p>
<p> rewriteengine on<br />
 rewriterule ^(.*)$ /index.html.en [R=301]</p>
<p> 或者更改为：</p>
<p> rewriteengine on<br />
 rewriterule ^(.*)$ http://192.168.1.xx/index.html.en [R=301]</p>
<p> (2).htaccess的实现方法</p>
<p> 我们将下面的部分放到httpd.conf中。</p>
<p> &lt; Directory “/usr/local/apache/htdocs/manual/”&gt; options Indexes followsymlinks</p>
<p> allowoverride all</p>
<p> &lt; /Directory &gt;</p>
<p> 然后将下面的部分放到/usr/local/apache/htdocs/manual/.htaccess中。</p>
<p> rewriteengine on<br />
 rewritebase /<br />
 rewriterule ^(.*)$ index.html.en [R=301]</p>
<p> 注: 对文件.htaccess所做的任何改动不需要重启动Apache。</p>
<p> 您还可以利用.htaccess方案将这个manual目录重定向到用户phpoa自己的主目录。</p>
<p> rewriteengine on<br />
 rewritebase /~phpoa/<br />
 rewriterule ^(.*)$ $1 [R=301]</p>
<p> 这样，对manual目录下任何文件的请求被重定向到～phpoa目录下相同文件的请求。</p>
<p> 2．将http://www.username.domain.com对于username的主页请求转换为对http://<br />
www.domain.com/username的请求</p>
<p> 对于HTTP/1.1的请求包括一个Host: HTTP头，我们能用下面的规则集重写http://<br />
www.username.domain .com/anypath到/home/username/anypath。</p>
<p> rewriteengine on<br />
 rewritecond %{HTTP_HOST} ^www.[^.] .host.com$<br />
 rewriterule ^(. ) %{HTTP_HOST}$1 [C]<br />
 rewriterule ^www.([^.] ).host.com(.*) /home/$1$2</p>
<p> 注: “rewritecond”表明是条件Apache Url Rewrite，当满足后面定义的条件后才会应用下面的重写规<br />
则，“rewritecond”有各种变量，请查阅相关文档。</p>
<p> 3．防火墙上的Apache Url Rewrite代理内部网段上服务器的请求</p>
<p> NameVirtualhost 1.2.3.4</p>
<p> &lt; Virtualhost 1.2.3.4:80 &gt; servername www.domain.com</p>
<p> rewriteengine on<br />
 proxyrequest on<br />
 rewriterule ^/(.*)$ http://192.168.1.3/$1 [P,L]</p>
<p> &lt; /Virtualhost &gt;</p>
<p> 注: 当外部浏览器请求http://www.domain.com时，将被解析到IP地址1.2.3.4，Apache交由mod_<br />
rewrite处理，转换成http://192.168.1.3/$1后再交由代理模块mod_proxy，得到内容后传送回用户的浏<br />
览器。</p>
<p> 4．基本预先设定的转换Map表进行重写rewritemap</p>
<p> 转换http://www.domain.com/{countrycode}/anypath到Map表中规定的URL，前面是虚拟主机中<br />
的定义。</p>
<p> rewritelog /usr/local/apache/logs/rewrite.log<br />
 rewriteloglevel 9<br />
 rewriteengine on<br />
 proxyrequest on<br />
 rewritemap sitemap txt:/usr/local/apache/conf/rewrite.map<br />
 rewriterule ^/([^/] ) /(.*)$ http://%{REMOTE_HOST}::$1 [C]<br />
 rewriterule (.*)::([a-z] )$ ${sitemap:$2|http://h.i.j.k/} [R=301,L]</p>
<p> 文件/usr/local/apache/conf/rewrite.map的内容如下:</p>
<p> sg http://a.b.c.d/<br />
 sh http://e.f.g.h/</p>
<p> 注: 当用户请求http://www.domain.com/sg/anypath时被重写为http://a.b.c.d/anypath。当<br />
需要调试时请用rewritelog和 rewriteloglevel 9联合，9为最大，即得到最多的调试信息；最小为1，表<br />
示得到最少的调试信息；默认为0，表示没有调试信息。</p>
<h2  class="related_post_title">与本文关系暧昧的文字</h2><ul class="related_post"><li><a href="http://www.linuxbyte.org/apache-license-version-2.html" title="Apache License Version 2.0">Apache License Version 2.0</a></li><li><a href="http://www.linuxbyte.org/config-apache-mod_limitipconn.html" title="apache 下限制单个IP 的并发连接数">apache 下限制单个IP 的并发连接数</a></li><li><a href="http://www.linuxbyte.org/centos5-apache2-php5-tomcat6-jdk.html" title="Centos5+Apache2.2.6+PHP5+tomcat6.0.14+jdk1.6整合安装 ">Centos5+Apache2.2.6+PHP5+tomcat6.0.14+jdk1.6整合安装 </a></li><li><a href="http://www.linuxbyte.org/tips-for-apache-php-security.html" title="apache,php安全小技巧">apache,php安全小技巧</a></li></ul><hr />
<p><small>© xiao H for <a href="http://www.linuxbyte.org">LinuxByte</a>, 2008<br />
Source: <a href="http://www.linuxbyte.org/apache-url-rewrite.html">Apache Url Rewrite(mod_rewrite)</a><br />
Post tags: <a href="http://www.linuxbyte.org/tag/apache" rel="tag">apache</a>, <a href="http://www.linuxbyte.org/tag/mod_rewrite" rel="tag">mod_rewrite</a>, <a href="http://www.linuxbyte.org/tag/url-rewrite" rel="tag">Url Rewrite</a>, <a href="http://www.linuxbyte.org/tag/url%e9%87%8d%e5%86%99" rel="tag">url重写</a>, <a href="http://www.linuxbyte.org/tag/url%e9%87%8d%e5%ae%9a%e5%90%91" rel="tag">url重定向</a><br/>
</small></p>
	<p style="margin:3px 0 0 0;">标签：<a href="http://www.linuxbyte.org/tag/apache" title="apache" rel="tag">apache</a>, <a href="http://www.linuxbyte.org/tag/mod_rewrite" title="mod_rewrite" rel="tag">mod_rewrite</a>, <a href="http://www.linuxbyte.org/tag/url-rewrite" title="Url Rewrite" rel="tag">Url Rewrite</a>, <a href="http://www.linuxbyte.org/tag/url%e9%87%8d%e5%86%99" title="url重写" rel="tag">url重写</a>, <a href="http://www.linuxbyte.org/tag/url%e9%87%8d%e5%ae%9a%e5%90%91" title="url重定向" rel="tag">url重定向</a></p>

<p><small>Feed enhanced by <a href='http://planetozh.com/blog/my-projects/wordpress-plugin-better-feed-rss/'>Better Feed</a> from  <a href='http://planetozh.com/blog/'>Ozh</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://www.linuxbyte.org/apache-url-rewrite.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

