Springboot整合tuckey实现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 >^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 >^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 标签内的表示匹配的模式,标签内的是想要转换的模式。

   <rule>
       <name>seo redirect 301</name>
       <condition >^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 标签包裹。

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

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


作者:007少侠

来源链接:

https://www.cnblogs.com/007sx/p/11758832.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值