---
title: "通过UrlRewriter实现网站链接静态化的方法"
author: "conkeyn"
date: "2009-04-28T17:21:47Z"
tags: ["Blog", "JSP", "Servlet", "Tomcat", "XML"]
slug: "urlrewriter"
draft: false
lastmod: "2009-04-28T17:21:47Z"
id: 376239
url: "https://conkeyn.iteye.com/blog/376239"
---

&lt;!-- \[if gte mso 9]&gt;&lt;xml&gt; &lt;w:WordDocument&gt; &lt;w:View&gt;Normal&lt;/w:View&gt; &lt;w:Zoom&gt;0&lt;/w:Zoom&gt; &lt;w:PunctuationKerning/&gt; &lt;w:DrawingGridVerticalSpacing&gt;7.8 磅&lt;/w:DrawingGridVerticalSpacing&gt; &lt;w:DisplayHorizontalDrawingGridEvery&gt;0&lt;/w:DisplayHorizontalDrawingGridEvery&gt; &lt;w:DisplayVerticalDrawingGridEvery&gt;2&lt;/w:DisplayVerticalDrawingGridEvery&gt; &lt;w:ValidateAgainstSchemas/&gt; &lt;w:SaveIfXMLInvalid&gt;false&lt;/w:SaveIfXMLInvalid&gt; &lt;w:IgnoreMixedContent&gt;false&lt;/w:IgnoreMixedContent&gt; &lt;w:AlwaysShowPlaceholderText&gt;false&lt;/w:AlwaysShowPlaceholderText&gt; &lt;w:Compatibility&gt; &lt;w:SpaceForUL/&gt; &lt;w:BalanceSingleByteDoubleByteWidth/&gt; &lt;w:DoNotLeaveBackslashAlone/&gt; &lt;w:ULTrailSpace/&gt; &lt;w:DoNotExpandShiftReturn/&gt; &lt;w:AdjustLineHeightInTable/&gt; &lt;w:BreakWrappedTables/&gt; &lt;w:SnapToGridInCell/&gt; &lt;w:WrapTextWithPunct/&gt; &lt;w:UseAsianBreakRules/&gt; &lt;w:DontGrowAutofit/&gt; &lt;w:UseFELayout/&gt; &lt;/w:Compatibility&gt; &lt;w:BrowserLevel&gt;MicrosoftInternetExplorer4&lt;/w:BrowserLevel&gt; &lt;/w:WordDocument&gt; &lt;/xml&gt;&lt;!\[endif]--&gt;&lt;!-- \[if gte mso 9]&gt;&lt;xml&gt; &lt;w:LatentStyles DefLockedState="false" LatentStyleCount="156"&gt; &lt;/w:LatentStyles&gt; &lt;/xml&gt;&lt;!\[endif]--&gt; &lt;!-- /* Font Definitions \*/ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"\\@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions \*/ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions \*/ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:2.0cm 2.0cm 2.0cm 2.0cm; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} --&gt; &lt;!-- \[if gte mso 10]&gt; &lt;style&gt; /* Style Definitions \*/ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} &lt;/style&gt; &lt;!\[endif]--&gt;

1 安装 tomcat/apache

2 下载 jk.so, 具体版本请根据需要，不再多说

3 关键是配置

httpd.conf

 

 

\# 去掉这个前面的 #, 启用它

LoadModule rewrite\_module modules/mod\_rewrite.so

 

\# 启用 jk, 同时配置参数

LoadModule   jk\_module   modules/mod\_jk.so      

JkWorkersFile   D:/Apache2.2/conf/workers.properties    

JkLogFile   d:/Apache2.2/logs/mod\_jk.log    

JkLogLevel   info  

#JkLogLevel   debug    

JkLogStampFormat   "\[%a   %b   %d   %H:%M:%S   %Y]   "    

JkOptions   +ForwardKeySize   +ForwardURICompat   -ForwardDirectories    

JkRequestLogFormat   "%w   %V   %T"    

JkMount   /servlet/*   myloadbalancer  

JkMount   /\*.jsp   myloadbalancer

 

\# 虚拟主机，这里模拟了 a.test.com 和 b.test.com 以及 a.tst.com/blog 的调用

 

&lt;VirtualHost \_default\_:80&gt;

ServerAdmin yourmail@domain.com

DocumentRoot D:\\Apache2.2\\htdocs

ServerName all-sites

ErrorLog logs/all-sites-error.log

CustomLog logs/all-sites-access.log common

RewriteEngine On

\# 下面三行实现动态解析

RewriteCond %{HTTP\_HOST} ^\[a-z0-9\\-]+\\.test\\.com$

RewriteRule ^/(.\*)$ /%{HTTP\_HOST}/$1

RewriteRule ^/(\[a-z0-9\\-]+)\\.test\\.com/?$ /index.jsp?u=$1 \[L,PT]

RewriteRule ^/(\[a-z0-9\\-]+)\\.test\\.com/blog(/(.\*))?$ /blog.jsp?u=$1&$3 \[L,PT]

 

&lt;Directory "D:\\Apache2.2\\htdocs"&gt;

    Options FollowSymLinks

    AllowOverride None

    Order allow,deny

    Allow from all

&lt;/Directory&gt;

 

&lt;/VirtualHost&gt;

 

workers.properties

 

  worker.list=myloadbalancer  

   

  worker.tomcat1.type=ajp13  

  worker.tomcat1.host=localhost  

  worker.tomcat1.port=8009  

  worker.tomcat1.lbfactor=1  

     

   

  worker.myloadbalancer.type=lb  

  worker.myloadbalancer.balance\_workers=tomcat1  

  worker.status.type=status 

 

server.xml

去掉前后的 &lt;!-- 和 --&gt; 启用 8009 端口

 

  &lt;Connector port="8009"

...

 

说明一下

在 httpd.conf 里面启用了 rewrite\_module, 和 mod\_jk, 并配置了 与 tomcat 的连接配置。

虚拟主机的配置是关键

ServerName all-sites

可以修改为

ServerName test.com

ServerAlias \*.test.com

这样可以明确指定这个虚拟主机负责的域名

urlRewrite 配置部分

 

    * RewriteCond %{HTTP\_HOST} ^\[a-z0-9\\-]+\\.test\\.com$  

    * RewriteRule ^/(.\*)$ /%{HTTP\_HOST}/$1

    * RewriteRule ^/(\[a-z0-9\\-]+)\\.test\\.com/?$ /index.jsp?u=$1 \[L,PT]  

    * RewriteRule ^/(\[a-z0-9\\-]+)\\.test\\.com/blog(/(.\*))?$ /blog.jsp?u=$1&$3 \[L,PT]  

 

第一行，声明这个重写只对主机名 (%{HTTP\_HOST}) 类似 XXXX.test.com  感兴趣，那个是正则的

第二行，将所有的对此域名的请求进行第一次改写，比如

a.test.com 改成

/a.test.com

把

b.test.com/blog 改成

/b.test/com/blog

第三行，对无参数的访问进行调整

将 /a.test.com 改成

/index.jsp?u=a

第四行，对有参数路径的进行调整

将 /b.test.com/blog 改成

/blog.jsp?u=b

如果还有参数比如

b.test.com/id=3 则最终改写为

/b.test.com/id=3

/blog.jsp?u=b&id=3

剩下的就不多说了！