UrlRewrite概念原理及使用方法解析
URL Rewrite即URL重寫(xiě),就是把傳入Web的請(qǐng)求重定向到其他URL的過(guò)程。URL Rewrite最常見(jiàn)的應(yīng)用是URL偽靜態(tài)化,是將動(dòng)態(tài)頁(yè)面顯示為靜態(tài)頁(yè)面方式的一種技術(shù)。比如http://www.123.com/news/index.asp?id=123 使用UrlRewrite轉(zhuǎn)換后可以顯示為 http://www.123.com/news/123.html
URL Rewrite有什么用?
1,首先是滿(mǎn)足觀感的要求。
對(duì)于追求完美主義的網(wǎng)站設(shè)計(jì)師,就算是網(wǎng)頁(yè)的地址也希望看起來(lái)盡量簡(jiǎn)潔明快。形如http://www.123.com/news/index.asp?id=123的網(wǎng)頁(yè)地址,自然是毫無(wú)美感可言,而用UrlRewrite技術(shù),你可以輕松把它顯示為 http://www.123.com/news/123.html。
2,其次可以隱藏網(wǎng)站所用的編程語(yǔ)言,還可以提高網(wǎng)站的可移植性。
當(dāng)網(wǎng)站每個(gè)頁(yè)面都掛著鮮明的.asp/.aspx/.php這種開(kāi)發(fā)語(yǔ)言的標(biāo)記,別人一眼即可看出你的網(wǎng)站是用什么語(yǔ)言做的。而且在改變網(wǎng)站的語(yǔ)言的時(shí)候,你需要改動(dòng)大量的鏈接。而且,當(dāng)一個(gè)頁(yè)面修改了擴(kuò)展名,它的pagerank也會(huì)隨之消失,從頭開(kāi)始。我們可以用UrlRewrite技術(shù)隱藏我們的實(shí)現(xiàn)細(xì)節(jié),這樣修改移植都很方便,而且完全不損失pagerank。
3,最后也是最重要的作用,是有利于搜索引擎更好地抓取你網(wǎng)站的內(nèi)容。
理論上,搜索引擎更喜歡靜態(tài)頁(yè)面形式的網(wǎng)頁(yè),搜索引擎對(duì)靜態(tài)頁(yè)面的評(píng)分一般要高于動(dòng)態(tài)頁(yè)面。所以,UrlRewrite可以讓我們網(wǎng)站的網(wǎng)頁(yè)更容易被搜索引擎所收錄。
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增加過(guò)濾器配置:
<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,點(diǎn)擊查看示例。
這里為了示例,我寫(xiě)了兩個(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頁(yè)面內(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)說(shuō)的很清楚
第一個(gè)功能是轉(zhuǎn)換,當(dāng)請(qǐng)求 /test/status/ 時(shí)實(shí)際請(qǐng)求到的是index.jsp
第二個(gè)功能是頁(yè)面顯示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頁(yè)面實(shí)際輸出HTML內(nèi)容為:
<html> <body> <a href="/f/test/status/" rel="external nofollow" >AAAAA</a> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- nginx rewrite 實(shí)現(xiàn)URL跳轉(zhuǎn)的方法
- IIS 7.5 使用URL Rewrite模塊的簡(jiǎn)單設(shè)置實(shí)現(xiàn)網(wǎng)頁(yè)跳轉(zhuǎn)
- IIS下配置頁(yè)面重寫(xiě)(配合插件url-rewrite2去除頁(yè)面后綴名)的實(shí)現(xiàn)方法
- Windows Server 2016中安裝PHP Manager、ARR3.0或者URL Rewrite 2.0無(wú)法成功的解決辦法
- IIS8中安裝和使用URL重寫(xiě)工具(URL Rewrite)的方法
- UrlRewrite 重寫(xiě)url詳解及實(shí)例
- 詳解nginx rewrite和根據(jù)url參數(shù)location
- windows server 2008R2系統(tǒng) IIS7.5配置偽靜態(tài)的方法(urlrewrite)
- Nginx服務(wù)器下使用rewrite重寫(xiě)url以實(shí)現(xiàn)偽靜態(tài)的示例
相關(guān)文章
DataList綁定到Row[]行集合的問(wèn)題的方法
DataList綁定到Row[]行集合的問(wèn)題的方法...2007-09-09Coolite Cool Study 1 在Grid中用ComboBox 來(lái)編輯數(shù)據(jù)
作為Coolite的第一個(gè)教程,我想展現(xiàn)給大家能夠體現(xiàn)Coolite強(qiáng)大的例子(當(dāng)然也比官方例子稍微復(fù)雜一點(diǎn))。2009-05-05ASP.NET 前臺(tái)javascript與后臺(tái)代碼調(diào)用
ASP.NET中前臺(tái)javascript與后臺(tái)代碼調(diào)用的實(shí)現(xiàn)代碼說(shuō)明。2009-08-08.net SMTP發(fā)送Email實(shí)例(可帶附件)
本文為大家詳細(xì)介紹下.net SMTP發(fā)送Email同時(shí)可帶附件的具體實(shí)現(xiàn)思路及代碼,想實(shí)現(xiàn)的朋友可以參考下哈,希望對(duì)大家有所幫助2013-07-07EntityFramework 6.x學(xué)習(xí)之多個(gè)上下文遷移實(shí)現(xiàn)分布式事務(wù)詳解
這篇文章主要給大家介紹了關(guān)于EntityFramework 6.x學(xué)習(xí)之多個(gè)上下文遷移實(shí)現(xiàn)分布式事務(wù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10asp.net 截取Http請(qǐng)求的實(shí)現(xiàn)代碼
本篇文章比較短,主要是因?yàn)槲业囊粋€(gè)隨想產(chǎn)生的一段代碼。 這段代碼的功能你可以叫做是簡(jiǎn)單的Http服務(wù)器也可以叫做Http請(qǐng)求截取。它實(shí)現(xiàn)的功能就是截取Http請(qǐng)求然后自己做處理。2010-06-06Asp.net core實(shí)現(xiàn)PushStream視頻流推送
這篇文章介紹了Asp.net core實(shí)現(xiàn)PushStream視頻流推送的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07