springboot开发的网站,怎么实现伪静态,提高百度收录

wylc123 1年前 ⋅ 580 阅读

1. 背景

        伪静态就是指动态网页通过重写URL的方法,实现去掉动态网页的参数,使之伪装成例如xxx.htm后缀结尾的纯静态路径,能够很好的让搜索引擎爬取程序识别(俗称:蜘蛛Spider),但是原有的动态路径还是可以照常访问。

1、伪静态能够很好让搜索引擎爬取程序索引并收录。而动态路径则由于参数过多而有可能被丢弃。

2、能够很好的促进站点内部的权重传递(原因还是动态路径不被搜索引擎看好,虽然搜索引擎目前已经有所改进,但相对来说依然不是那么友好,如果丢弃那么就不予计算权重。)

伪静态路径总的来说,是非常有利于搜索引擎爬取和收录的。所以这个办法就很好的解决了,由于搜索引擎目前对动态路径不友好,动态路径索引困难的问题。

2. 解决方案

springboot中配置urlrewrite实现url伪静态强化网站seo

3. 详细方案

3.1 关于urlrewrite

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

3.2 编码实现

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

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

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


相关文章推荐

全部评论: 0

    我有话说: