关于urlrewrite

urlrewrite使用强大的自定义规则来使用用户更容易记住、搜索引擎更容易找到的URL(对于seo比较重要)。通过使用规则模板、重写映射,Web管理员可以轻松地设置规则,根据HTTP标头、HTTP响应或请求标头、变量,甚至复杂的编程规则来定义URL重写行为。此外,Web管理员可以根据重写规则中表示的逻辑进行url重定向、发送自定义响应或停止HTTP请求。

为何有这篇教程

百度上查询urlrewrite这个工具包的使用教程时,网上并没有springboot整合的完整示例,于是就自己摸索的写了一下。

开始:

1.引入maven依赖:

<dependency>
<groupId>org.tuckey</groupId>
<artifactId>urlrewritefilter</artifactId>
<version>4.0.4</version>
</dependency>

2.重写UrlRewriteFilter的过滤器加载urlrewrite.xml规则,urlrewrite.xml放在 resources文件夹下

package webapp.conf;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.tuckey.web.filters.urlrewrite.Conf;
import org.tuckey.web.filters.urlrewrite.UrlRewriteFilter; import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import java.io.IOException; @Configuration
public class UrlRewriteConf extends UrlRewriteFilter {
private static final String URL_REWRITE = "classpath:/urlrewrite.xml"; //注入urlrewrite配置文件
@Value(URL_REWRITE)
private Resource resource; //重写配置文件加载方式
protected void loadUrlRewriter(FilterConfig filterConfig) throws ServletException {
try {
//将Resource对象转换成Conf对象
Conf conf = new Conf(filterConfig.getServletContext(), resource.getInputStream(), resource.getFilename(), "@@traceability@@");
checkConf(conf);
} catch (IOException ex) {
throw new ServletException("Unable to load URL rewrite configuration file from " + URL_REWRITE, ex);
}
}
}

3.urlrewrite.xml文件配置,urlrewrite.xml规则示例:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN"
"http://tuckey.org/res/dtds/urlrewrite3.2.dtd">
<urlrewrite>
<rule>
<name>seo redirect 301</name>
<condition name="host">^9191boke.com$</condition>
<from>^/(.*)</from>
<to type="permanent-redirect" last="true">http://www.9191boke.com/$1</to>
</rule>
<rule>
<name>seo redirect 301</name>
<condition name="host">^localhost:9191$</condition>
<from>^/(.*)</from>
<to type="permanent-redirect" last="true">http://www.localhost:9191/$1</to>
</rule>
<rule>
<from>^/([0-9]+).html$</from>
<to>/blogdetails/$1.html</to>
</rule>
<rule>
<from>^/p_([0-9]+).html$</from>
<to>/?page=$1</to>
</rule>
</urlrewrite>

from标签内的表示匹配的模式,<to>标签内的是想要转换的模式。

<rule>

<name>seo redirect 301</name>

<condition name="host">^9191boke.com$</condition>

<from>^/(.*)</from>

<to type="permanent-redirect" last="true">http://www.9191boke.com/$1</to>

</rule>

以上为域名301重定向,所有http(s)://9191boke.com/xxx链接会重定向至http://www.9191boke.com/xxx

<from>^/([0-9]+).html$</from>

<to>/blogdetails/$1.html</to>

^/([0-9]+).html$表示http://xxx/数字.html会发送实际请求为http://xxx/blogdetails/数字.html

<rule>

<from>^/p_([0-9]+).html$</from>

<to>/?page=$1</to>

</rule>

^/p_([0-9]+).html$表示http://xxx/p_数字.html会发送实际请求为http://xxx/?page=数字.html

每一条拦截规则使用rule标签包裹。

这里没有特别需要注意的地方,如果只是简单的使用的话,记住下面这一点就足够了。

如果你会用正则表达式的话,可以通过正则表达式来处理(推荐使用),如果不会,可以使用通配符。

最新文章

  1. C#开发微信门户及应用(31)--微信语义理解接口的实现和处理
  2. 【转】NGUI研究院之三种方式监听NGUI的事件方法(七)
  3. Elasticsearch推荐插件篇(head,sense,marvel)
  4. 阿里社招B2B
  5. IDEA SSH
  6. hdu_1403_Longest Common Substring(后缀数组的应用)
  7. include和 merge
  8. 修改TabPageIndicator下划线的颜色
  9. 【Spring】整合SpringMVC、MyBatis
  10. yum安装mysql5.7
  11. 【慕课网实战】八、以慕课网日志分析为例 进入大数据 Spark SQL 的世界
  12. Java 8 新特性:3-函数(Function)接口
  13. node.js api文档生成
  14. mysql的innodb存储引擎
  15. js多选下拉框
  16. Selenium+Java元素定位之一
  17. C#,清晨随手写
  18. 常见的浏览器端的存储技术:cookie
  19. MongoDB 进阶
  20. centos7.1 从源码升级安装Python3.5.2

热门文章

  1. 在分页中,删除操作后,AJAX重载刷新当前页
  2. ifream
  3. oracle-DBlink基本操作
  4. [最新].NET Core ORM 开源项目一览,持续更新
  5. nodejs调用cmd命令
  6. 大话设计模式Python实现-代理模式
  7. tomcat 下 base64图片上传超过2m的解决方案
  8. hibernate中many-to-one的not-found属性和@notfound注解
  9. sql server生成随机id
  10. golang --strings 下常用函数api