之前做的网站基本成型,不过,听说要对搜索引擎友好点,还得要将网站弄成静态化,至少要做点“伪静态”的工作。而我对url rewrite早有所闻,于是乎就上
其官网 下了个来玩。入门很简单,下载地址是
http://tuckey.org/urlrewrite/dist/urlrewritefilter-2.6.zip ,下载来解压。将解压出来的urlrewrite-2.6.0.jar复制到项目中的WEB-INF\lib下,还有urlrewrite.xml复制到WEB-INF下。
接下来就是配置了,配置也是超级简单的。
web.xml里的配置如下,在urlrewrite.xml里面也有注释说明怎么添加的。
<!– UrlRewriteFilter –>
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>
org.tuckey.web.filters.urlrewrite.UrlRewriteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
注意将这一段配置放在struts2的配置上面。还要在struts2的配置里加上两行,具体如下:
<!– 配置struts2 –>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
接下来是修改配置urlrewrite.xml里的内容,它的规则如下:
<urlrewrite>
<rule>
<note>
The rule means that requests to /test/status/ will be redirected to /rewrite-status
the url will be rewritten.
</note>
<from>/test/status/</from>
<to type="redirect">%{context-path}/rewrite-status</to>
</rule>
<outbound-rule>
<note>
The outbound-rule specifies that when response.encodeURL is called (if you are using JSTL c:url)
the url /rewrite-status will be rewritten to /test/status/.
The above rule and this outbound-rule means that end users should never see the
url /rewrite-status only /test/status/ both in thier location bar and in hyperlinks
in your pages.
</note>
<from>/rewrite-status</from>
<to>/test/status/</to>
</outbound-rule>
</urlrewrite>
只要你自己把需要重写的URL规则写上去就好了,我的一个例子就是:
<rule>
<note>shop</note>
<from>/shops.html</from>
<to>/shops/findShops.action</to>
</rule>
这样访问shops.html就是访问/shops/findShops.action。