這篇文章主要講解了“UrlRewrite概念和使用方法”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“UrlRewrite概念和使用方法”吧!
URL Rewrite即URL重寫,就是把傳入Web的請(qǐng)求重定向到其他URL的過程。URL Rewrite最常見的應(yīng)用是URL偽靜態(tài)化,是將動(dòng)態(tài)頁面顯示為靜態(tài)頁面方式的一種技術(shù)。比如http://www.123.com/news/index.asp?id=123 使用UrlRewrite轉(zhuǎn)換后可以顯示為 http://www.123.com/news/123.html
URL Rewrite有什么用?
1,首先是滿足觀感的要求。
對(duì)于追求完美主義的網(wǎng)站設(shè)計(jì)師,就算是網(wǎng)頁的地址也希望看起來盡量簡(jiǎn)潔明快。形如http://www.123.com/news/index.asp?id=123的網(wǎng)頁地址,自然是毫無美感可言,而用UrlRewrite技術(shù),你可以輕松把它顯示為 http://www.123.com/news/123.html。
2,其次可以隱藏網(wǎng)站所用的編程語言,還可以提高網(wǎng)站的可移植性。
當(dāng)網(wǎng)站每個(gè)頁面都掛著鮮明的.asp/.aspx/.php這種開發(fā)語言的標(biāo)記,別人一眼即可看出你的網(wǎng)站是用什么語言做的。而且在改變網(wǎng)站的語言的時(shí)候,你需要改動(dòng)大量的鏈接。而且,當(dāng)一個(gè)頁面修改了擴(kuò)展名,它的pagerank也會(huì)隨之消失,從頭開始。我們可以用UrlRewrite技術(shù)隱藏我們的實(shí)現(xiàn)細(xì)節(jié),這樣修改移植都很方便,而且完全不損失pagerank。
3,最后也是最重要的作用,是有利于搜索引擎更好地抓取你網(wǎng)站的內(nèi)容。
理論上,搜索引擎更喜歡靜態(tài)頁面形式的網(wǎng)頁,搜索引擎對(duì)靜態(tài)頁面的評(píng)分一般要高于動(dòng)態(tài)頁面。所以,UrlRewrite可以讓我們網(wǎng)站的網(wǎng)頁更容易被搜索引擎所收錄。
Java方面,參考使用:UrlRewriteFilter,地址:http://tuckey.org/urlrewrite/。
官方簡(jiǎn)介: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>
關(guān)于配置的更多信息點(diǎn)擊這里!
3.增加urlrewrite.xml到你的WEB-INF,點(diǎn)擊查看示例。
這里為了示例,我寫了兩個(gè)功能的節(jié)點(diǎn)配置:
<?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="/tupian/20230522/ <html> <body> <c:url var="myURL" value="/rewrite-status" /> <a href="${myURL }" rel="external nofollow" >AAAAA</a> </body> </html>
Note已經(jīng)說的很清楚
第一個(gè)功能是轉(zhuǎn)換,當(dāng)請(qǐng)求 /test/status/ 時(shí)實(shí)際請(qǐng)求到的是index.jsp
第二個(gè)功能是頁面顯示URL的轉(zhuǎn)換,這里必須使用JSTL c:url,將value部分轉(zhuǎn)換為指定路徑,達(dá)到屏蔽URL的功能
4.實(shí)際效果
當(dāng)請(qǐng)求 /test/status/ 時(shí)實(shí)際請(qǐng)求到的是index.jsp
index.jsp頁面實(shí)際輸出HTML內(nèi)容為:
<html> <body> <a href="/f/test/status/" rel="external nofollow" >AAAAA</a> </body> </html>
感謝各位的閱讀,以上就是“UrlRewrite概念和使用方法”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)UrlRewrite概念和使用方法這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
網(wǎng)站欄目:UrlRewrite概念和使用方法-創(chuàng)新互聯(lián)
地址分享:http://aaarwkj.com/article14/ccjhge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、服務(wù)器托管、軟件開發(fā)、網(wǎng)站導(dǎo)航、網(wǎng)站策劃、微信公眾號(hào)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容