springboot僞靜態

在平常網站訪問中,會把動態地址改形成僞靜態地址。html

例如: 訪問新聞欄目 /col/1/,這是原有地址,若是這樣訪問,不利於搜索引擎檢索收錄,同時安全性也不是很好。java

改造以後:web

/col/1.html。spring

改造方法:安全

1.添加urlrewritefilteride

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

2.配置beanpost

複製代碼
import java.io.IOException;

import javax.servlet.FilterConfig;
import javax.servlet.ServletException;

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;

@Configuration
public class UrlRewriteFilterConfig extends UrlRewriteFilter {

  private static final String URL_REWRITE = "classpath:/urlrewrite.xml";

  // Inject the Resource from the given location
  @Value(URL_REWRITE)
  private Resource resource;

  // Override the loadUrlRewriter method, and write your own implementation
  protected void loadUrlRewriter(FilterConfig filterConfig) throws ServletException {
    try {
      // Create a UrlRewrite Conf object with the injected resource
      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);
    }
  }
}
複製代碼

參考網址:http://blog.jdriven.com/2016/02/urlrewritefilter-load-configuration-with-spring-resourceloader/測試

3.配置urlrewrite.xml網站

複製代碼
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
        "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">

<urlrewrite>

    <!-- 欄目首頁 -->
    <rule>
        <from>^/col/(\w+)\.html$</from>
        <to>/col/$1/</to>
    </rule>

    <!-- 欄目列表頁,注意html後面沒有加$,由於後面還有若干參數 -->
    <rule>
        <from>^/col/list/(\w+)/(\w+)\.html</from>
        <to>/col/list/$1/$2/</to>
    </rule>

    <!-- 文章詳情頁 -->
    <rule>
        <from>^/art/(\w+)\.html$</from>
        <to>/art/$1/</to>
    </rule>
    
    <!-- 靜態網頁 -->
    <rule>
        <from>^/static/(\w+)\.html$</from>
        <to>/static/$1/</to>
    </rule>

</urlrewrite>
複製代碼

image

配置說明請參考:http://blog.163.com/zhangmihuo_2007/blog/static/27011075201351433716225/搜索引擎

至此配置完畢,啓動測試,注意看紅框處,說明加載了urlwrite。

image

訪問頁面以下,成功了!

image

相關文章
相關標籤/搜索