springboot中配置urlrewrite实现url伪静态强化网站seo
关于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标签包裹。
这里没有特别需要注意的地方,如果只是简单的使用的话,记住下面这一点就足够了。
如果你会用正则表达式的话,可以通过正则表达式来处理(推荐使用),如果不会,可以使用通配符。
springboot中配置urlrewrite实现url伪静态强化网站seo的更多相关文章
- 在SpringBoot中配置aop
前言 aop作为spring的一个强大的功能经常被使用,aop的应用场景有很多,但是实际的应用还是需要根据实际的业务来进行实现.这里就以打印日志作为例子,在SpringBoot中配置aop 已经加入我 ...
- springboot中配置主从redis
测试redis的主从配置 redis实例 文件夹名称如下 redis_master_s redis_slaver1_s redis_slaver2_s redis.conf文件 master的redi ...
- SpringBoot(八):SpringBoot中配置字符编码 Springboot中文乱码处理
SpringBoot中配置字符编码一共有两种方式 方式一: 使用传统的Spring提供的字符编码过滤器(和第二种比较,此方式复杂,由于时间原因这里先不介绍了,后续补上) 方式二(推荐使用) 在appl ...
- 在SpringBoot中配置定时任务
前言 之前在spring中使用过定时任务,使用注解的方式配置很方便,在SpringBoot中的配置基本相同,只是原来在spring中的xml文件的一些配置需要改变,在SpringBoot中也非常简单. ...
- springboot中配置过滤器以及可能出现的问题
在springboot添加过滤器有两种方式: 1.通过创建FilterRegistrationBean的方式(建议使用此种方式,统一管理,且通过注解的方式若不是本地调试,如果在filter中需要增加c ...
- Mybatis源码解读-SpringBoot中配置加载和Mapper的生成
本文mybatis-spring-boot探讨在springboot工程中mybatis相关对象的注册与加载. 建议先了解mybatis在spring中的使用和springboot自动装载机制,再看此 ...
- Maven 中配置 Urlrewrite 基本配置
1. 在maven项目的pom.xml文件里加入: <!-- URL Rewrite --> <dependency> <groupId>org.tuckey< ...
- SpringBoot中配置起动时的数据库初始化角本
一.简介 我们使用SpringBoot + JPA时,需要程序在启动时执行数据表的初始化或者数据库记录的初始化.一般数据表的初始化可以通过在Spring Boot的application.proper ...
- springboot中配置tomcat的access log
在tomcat的access中打印出请求的情况可以帮助我们分析问题,通常比较关注的有访问IP.线程号.访问url.返回状态码.访问时间.持续时间. 在Spring boot中使用了内嵌的tomcat, ...
随机推荐
- Arduino SPI驱动7引脚0.96寸OLED SSD1306 调试笔记
https://www.geek-workshop.com/thread-37818-1-1.html 2.下载最新库https://learn.adafruit.com/monoc ... ibra ...
- 使用jdk 容器镜像注意默认编码问题
最近在使用一个开源数据pipeline 处理的工具的时候,发现了jdk容器镜像编码的一些问题 以下是一个简单的描述 问题 使用了streamsets 工具,使用容器运行,默认使用了adoptopenj ...
- __str__与__repr__的触发顺序总结
1.__str__是个内置的方法,无需使用者去调用,其会在满足某一条件时自动触发.那么要触发它运行都有哪些条件呢? 有三种条件,分别为:print , str , %s 2.__repr__同样是个内 ...
- Tomcat服务部署与Nginx负载均衡配置
一.中间键产品介绍 目前来说IBM的WebSphere,Oracle的Weblogic占据了市场上java语言Web站点的部分份额,该两种软件都是商业化的软件,由于性能优越,可靠性高等优点应用于大型互 ...
- [LeetCode] 527. Word Abbreviation 单词缩写
Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...
- 第三次实验报告:使用Packet Tracer分析TCP连接建立过程
目录 1 实验目的 2 实验内容 3. 实验报告 3.1 建立网络拓扑结构 3.2 配置参数 3.3 抓包,分析TCP连接建立过程 4. 拓展 (不作要求,但属于加分项) 1 实验目的 使用路由器连接 ...
- fiddler抓包-5-Composer功能进行接口测试
前言 fiddler是个强大的抓接口工具,轻松看出接口的所有参数,这里介绍一个Composer功能它也可以进行接口测试,平时接口可能传参错误,我们可以拖拽接口来改参数直接再请求了,非常方便! 一.Co ...
- Arcpy中Geometry类与Array类转换的陷阱
1.现象说明 使用Arcpy.da.searchcursor得到Geometry,将Geometry转换成Array,再从Array转换回Geometry.若Geometry包含内环,这个过程可能导致 ...
- Delphi 10.3.2 社区版的安装
1.去 https://www.embarcadero.com/cn/products/delphi/starter 下载安装程序 首先你要有一个embarcadero社区账号,国内线路下载有点慢,下 ...
- 深入V8引擎-AST(5)
懒得发首页了,有时候因为贴的代码太多会被下,而且这东西本来也只是对自己学习的记录,阅读体验极差,所以就本地自娱自乐的写着吧! 由于是解析字符串,所以在开始之前介绍一下词法结构体中关于管理字符串类的属性 ...