urlRewriteFilter

urlRewriteFileter 是apache的一个jar包,主要功能如其名:实现url的重定向。

其他功能参考其相关文档,截取部分入下:

  • URL Tidyness / URL Abstraction - keep URLs tidy irrespective of the underlying technology or framework (JSP, Servlet, Struts etc).
  • Browser Detection - Allows you to rewrite URLs based on request HTTP headers (such as user-agent or charset).
  • Date based rewriting - Allows you to forward or redirect to other URL's based on the date/time (good for planned outages).
  • Moved content - enable a graceful move of content or even a change in CMS.
  • Tiny/Friendly URL's (i.e. blah.com/latest can be redirected to blah.com/download/ver1.2.46.2/setup.exe)
  • A Servlet mapping engine (see Method Invocation)
(1)在mvn中配置urlRewriteFilter:

    org.tuckeyurlrewritefilter4.0.3
(2)配置方式

urlRewriteFilter本质还是一个filter,配置在Spring的web.xml里面,例如:

   UrlRewriteFilter

   org.tuckey.web.filters.urlrewrite.UrlRewriteFilter

       
            confPath
            /WEB-INF/url-rewrite.xml
        

   UrlRewriteFilter

   /*

如上配置表示UrlRewriteFilter 这个filter可以匹配到所有的url,具体的重定向规则见如上黄底红体字, 它表示了对UrlRewriteFilter的初始化,即对其

confPath参数赋值“WEB-INF/url-rewrite.xml”, 详细可见UrlRewriteFilter的源码,参见附录。

(3)urlrewrite.xml文件

关于url-rewrite.xml文件的写法,可以参考如下示例, 尤其注意正则表达式的匹配:

#首先是文件头


        "http://tuckey.org/res/dtds/urlrewrite2.6.dtd">

#其次urlrewrite节点

#再次是rule节点
 
    /webplay3-([0-9]+)-([0-9]+).xml
    /play/v1/getchannel/$2?enableFb=0&pre=ikan&dt=1&from=2
 
 
    /(v3|1|-1)/chplay-([0-9]+)-([0-9]+)\.xml
    /play/v1/getchannel/$3?enableFb=1&pre=client&dt=0&from=1
 


具体还可参考如下博客:http://aumy2008.blogbus.com/logs/42495983.html


附:UrlRewriteFilter的源代码init方法:

  public void init(FilterConfig filterConfig)
    throws ServletException
  {
    log.debug("filter init called");
    if (filterConfig == null) {
      log.error("unable to init filter as filter config is null");
      return;
    }

    log.debug("init: calling destroy just in case we are being re-inited uncleanly");
    destroyActual();

    this.context = filterConfig.getServletContext();
    if (this.context == null) {
      log.error("unable to init as servlet context is null");
      return;
    }

    Log.setConfiguration(filterConfig);

    String confReloadCheckIntervalStr = filterConfig.getInitParameter("confReloadCheckInterval");
    String confPathStr = filterConfig.getInitParameter("confPath");
    String statusPathConf = filterConfig.getInitParameter("statusPath");
    String statusEnabledConf = filterConfig.getInitParameter("statusEnabled");
    String statusEnabledOnHosts = filterConfig.getInitParameter("statusEnabledOnHosts");

    String allowConfSwapViaHttpStr = filterConfig.getInitParameter("allowConfSwapViaHttp");
    if (!(StringUtils.isBlank(allowConfSwapViaHttpStr))) {
      this.allowConfSwapViaHttp = "true".equalsIgnoreCase(allowConfSwapViaHttpStr);
    }

    if (!(StringUtils.isBlank(confReloadCheckIntervalStr)))
    {
      this.confReloadCheckInterval = (1000 * NumberUtils.stringToInt(confReloadCheckIntervalStr));

      if (this.confReloadCheckInterval < 0) {
        this.confReloadCheckEnabled = false;
        log.info("conf reload check disabled");
      }
      else if (this.confReloadCheckInterval == 0) {
        this.confReloadCheckEnabled = true;
        log.info("conf reload check performed each request");
      }
      else {
        this.confReloadCheckEnabled = true;
        log.info("conf reload check set to " + (this.confReloadCheckInterval / 1000) + "s");
      }
    }
    else {
      this.confReloadCheckEnabled = false;
    }

    String modRewriteConf = filterConfig.getInitParameter("modRewriteConf");
    if (!(StringUtils.isBlank(modRewriteConf))) {
      this.modRewriteStyleConf = "true".equals(StringUtils.trim(modRewriteConf).toLowerCase());
    }

    if (!(StringUtils.isBlank(confPathStr)))
      this.confPath = StringUtils.trim(confPathStr);
    else
      this.confPath = ((this.modRewriteStyleConf) ? "/WEB-INF/.htaccess" : "/WEB-INF/urlrewrite.xml");

    log.debug("confPath set to " + this.confPath);

    if ((statusEnabledConf != null) && (!("".equals(statusEnabledConf)))) {
      log.debug("statusEnabledConf set to " + statusEnabledConf);
      this.statusEnabled = "true".equals(statusEnabledConf.toLowerCase());
    }
    if (this.statusEnabled)
    {
      if ((statusPathConf != null) && (!("".equals(statusPathConf)))) {
        this.statusPath = statusPathConf.trim();
        log.info("status display enabled, path set to " + this.statusPath);
      }
    }
    else { log.info("status display disabled");
    }

    if (StringUtils.isBlank(statusEnabledOnHosts))
      statusEnabledOnHosts = "localhost, local, 127.0.0.1";
    else
      log.debug("statusEnabledOnHosts set to " + statusEnabledOnHosts);

    this.statusServerNameMatcher = new ServerNameMatcher(statusEnabledOnHosts);

    String modRewriteConfText = filterConfig.getInitParameter("modRewriteConfText");
    if (!(StringUtils.isBlank(modRewriteConfText))) {
      ModRewriteConfLoader loader = new ModRewriteConfLoader();
      Conf conf = new Conf();
      loader.process(modRewriteConfText, conf);
      conf.initialise();
      checkConf(conf);
      this.confLoadedFromFile = false;
    }
    else
    {
      loadUrlRewriter(filterConfig);
    }
  }


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部