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标签包裹。

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

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

posted @ 2019-10-29 15:03  007少侠  阅读(2526)  评论(0编辑  收藏  举报
友情链接:便宜vps(CN2GIA线路)
站长统计: