當前位置:首頁 >  站長 >  編程技術 >  正文

UrlRewrite概念原理及使用方法解析

 2020-10-23 13:48  來源: 腳本之家   我來投稿 撤稿糾錯

  域名預訂/競價,好“米”不錯過

這篇文章主要介紹了UrlRewrite概念原理及使用方法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

URL Rewrite即URL重寫,就是把傳入Web的請求重定向到其他URL的過程。URL Rewrite最常見的應用是URL偽靜態(tài)化,是將動態(tài)頁面顯示為靜態(tài)頁面方式的一種技術。比如http://www.123.com/news/index.asp?id=123 使用UrlRewrite轉換后可以顯示為 http://www.123.com/news/123.html

URL Rewrite有什么用?

1,首先是滿足觀感的要求。

對于追求完美主義的網(wǎng)站設計師,就算是網(wǎng)頁的地址也希望看起來盡量簡潔明快。形如http://www.123.com/news/index.asp?id=123的網(wǎng)頁地址,自然是毫無美感可言,而用UrlRewrite技術,你可以輕松把它顯示為 http://www.123.com/news/123.html。

2,其次可以隱藏網(wǎng)站所用的編程語言,還可以提高網(wǎng)站的可移植性。

當網(wǎng)站每個頁面都掛著鮮明的.asp/.aspx/.php這種開發(fā)語言的標記,別人一眼即可看出你的網(wǎng)站是用什么語言做的。而且在改變網(wǎng)站的語言的時候,你需要改動大量的鏈接。而且,當一個頁面修改了擴展名,它的pagerank也會隨之消失,從頭開始。我們可以用UrlRewrite技術隱藏我們的實現(xiàn)細節(jié),這樣修改移植都很方便,而且完全不損失pagerank。

3,最后也是最重要的作用,是有利于搜索引擎更好地抓取你網(wǎng)站的內(nèi)容。

理論上,搜索引擎更喜歡靜態(tài)頁面形式的網(wǎng)頁,搜索引擎對靜態(tài)頁面的評分一般要高于動態(tài)頁面。所以,UrlRewrite可以讓我們網(wǎng)站的網(wǎng)頁更容易被搜索引擎所收錄。

Java方面,參考使用:UrlRewriteFilter,地址:http://tuckey.org/urlrewrite/。

官方簡介:A Java Web Filter for any compliant web application servers (such as Tomcat, JBoss, Jetty or Resin), which allows you to rewrite URLs before they get to your code. It is a very powerful tool just like Apache's mod_rewrite!

1.增加Jar包urlrewritefilter-4.0.3.jar到Lib

2.在web.xml增加過濾器配置:

<filter>
  <filter-name>UrlRewriteFilter</filter-name>
  <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>UrlRewriteFilter</filter-name>
  <url-pattern>/*</url-pattern>
  <dispatcher>REQUEST</dispatcher>
  <dispatcher>FORWARD</dispatcher>
</filter-mapping>

3.增加urlrewrite.xml到你的WEB-INF

這里為了示例,我寫了兩個功能的節(jié)點配置:

<?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>
    <note>
      The rule means that requests to /test/status/ will be redirected to 
      /rewrite-status
      the url will be rewritten.
    </note>
    <from>/test/status/</from>
    <to type="redirect">%{context-path}/index.jsp</to>
  </rule>
  <outbound-rule>
    <note>
      The outbound-rule specifies that when response.encodeURL is called 
      (if you are using JSTL c:url)
      the url /rewrite-status will be rewritten to /test/status/.
 
      The above rule and this outbound-rule means that end users should never see the
      url /rewrite-status only /test/status/ both in thier location bar and in hyperlinks
      in your pages.
    </note>
    <from>/rewrite-status</from>
    <to>/test/status/</to>
  </outbound-rule>
</urlrewrite>

index.jsp頁面內(nèi)容如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<html>
 <body>
   <c:url var="myURL" value="/rewrite-status" />
   <a href="${myURL }" rel="external nofollow" >AAAAA</a>
 </body>
</html>

Note已經(jīng)說的很清楚

第一個功能是轉換,當請求 /test/status/ 時實際請求到的是index.jsp

第二個功能是頁面顯示URL的轉換,這里必須使用JSTL c:url,將value部分轉換為指定路徑,達到屏蔽URL的功能

4.實際效果

當請求 /test/status/ 時實際請求到的是index.jsp

index.jsp頁面實際輸出HTML內(nèi)容為:

<html>
 <body>
   <a href="/f/test/status/" rel="external nofollow" >AAAAA</a>
 </body>
</html>

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

來源:腳本之家

鏈接:https://www.jb51.net/article/197917.htm

申請創(chuàng)業(yè)報道,分享創(chuàng)業(yè)好點子。點擊此處,共同探討創(chuàng)業(yè)新機遇!

相關文章

熱門排行

信息推薦