通过UrlRewriter实现网站链接静态化的方法
<!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--> <!-- /* 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;} --> <!-- [if gte mso 10]> <style> /* 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;} </style> <![endif]-->
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 的调用
<VirtualHost _default_:80>
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]
<Directory "D:\Apache2.2\htdocs">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
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
去掉前后的 <!-- 和 --> 启用 8009 端口
<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
剩下的就不多说了!