tomcat配置301重定向


为了让公司域名统一化,就需要把其他的域名跳转到www.wyy.com

1.简介

     urlRewriteFilter是一个用于改写URL的Web过滤器,类似于Apache的mod_rewrite。适用于任何Web应用服务器(如Tomcat,jboss,jetty,Resin,Orion等)。其典型应用就把动态URL静态化,便于搜索引擎爬虫抓取你的动态网页。

 

2.下载

wget http://urlrewritefilter.googlecode.com/files/urlrewritefilter-4.0.3.jar

并放入tomcat的 WEB-INF/lib下

3.配置tomcat

编辑WEB-INF/web.xml 在其它servlet mapping前加入

 <!--301地址重定向-->
  <filter>
  <filter-name>UrlRewriteFilter</filter-name>
  <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<init-param>
        <param-name>confReloadCheckInterval</param-name>
        <param-value>60</param-value>
    </init-param>
<init-param>
        <param-name>confPath</param-name>
        <param-value>/WEB-INF/urlrewite.xml</param-value>
    </init-param>
 
    <!-- sets up log level (will be logged to context log)
    can be: slf4j, TRACE, DEBUG, INFO (default), WARN, ERROR, FATAL,
    sysout:{level} (ie, sysout:DEBUG)
    if you are having trouble using normal levels use sysout:DEBUG
    (default WARN) 设置日志的等级-->
    <init-param>
        <param-name>logLevel</param-name>
        <param-value>DEBUG</param-value>
    </init-param>
 
    <!-- you can change status path so that it does not
    conflict with your installed apps (note, defaults
    to /rewrite-status) note, must start with /设置状态目录,必须以/开始,默认为/rewrite-status -->
    <init-param>
        <param-name>statusPath</param-name>
        <param-value>/status</param-value>
    </init-param>
 
    <!-- you can disable status page if desired
    can be: true, false (default true) 是否允许状态页面,默认为true-->
    <init-param>
        <param-name>statusEnabled</param-name>
        <param-value>true</param-value>
    </init-param>
 
</filter>
<filter-mapping>
  <filter-name>UrlRewriteFilter</filter-name>
  <url-pattern>/*</url-pattern>
  <dispatcher>REQUEST</dispatcher>
  <dispatcher>FORWARD</dispatcher>
</filter-mapping>


 

 

4.添加跳转规则

配置文件规则: 

跳转规则是在condition匹配前提下才进行重定向。

在WEB-INF下新建urlrewite.xml文件,加入跳转规则

<urlrewrite>
  <rule>
      <name>seo redirect</name>
      <condition name="host" operator="notequal">^123.57.155.155</condition>
      <condition name="host" operator="notequal">^www.wyy.com</condition>
      <from>^/(.*)</from>
      <to type="permanent-redirect" last="true">https://www.wyy.com/$1</to>
  </rule>
</urlrewrite>



上面是公司要求的,要求可以正常通过IP来访问服务器,以上配置说明可以通过123.57.155.155和www.wyy.com不跳转以外的url输入都会跳转到www.wyy.com

参考官方文档:

http://cdn.rawgit.com/paultuckey/urlrewritefilter/master/src/doc/manual/4.0/index.html