本文主要介紹url重寫可以用什么接口(url重寫sessionid)?,下面一起看看url重寫可以用什么接口(url重寫sessionid)?相關資訊。
本章主要介紹如何用lua重寫url。其實url重寫也可以通過nginx來完成,但是當重寫規(guī)則比較復雜的時候,使用nginx就沒那么方便了。lua可以輕松解決這里用到的幾個核心api,即ngx.redirect和ngx.exec。
ngx.redirect,顧名思義,就是執(zhí)行重定向動作。重定向?qū)е聈rl更改,返回302狀態(tài)代碼,瀏覽器將重新啟動對重定向url的新請求。用法很簡單。
舊的uri/索引/文章?id = 10000 ngx . redirect( ;/article ;..ngx.var.is_args..ngx.var.args)新uri/文章?id=10000 ngx.exec內(nèi)部直接完成請求,直接返回內(nèi)容。網(wǎng)址不會變,用法和上面差不多。
舊的uri/索引/文章?id = 10000 ngx . exec( ;/article ;..ngx.var.is_args..ngx.var.args)新的uri/索引/文章?id=10000為了讓url重寫寫在一個地方,便于維護,我們可以對之前封裝的mvc框架進行擴展。
添加這段代碼
lite/mvc.lua
- url重寫開始本地ret,rewrite = pcall (require, 重寫 )-安全引入重寫模塊,如果不是則不會給出錯誤如果ret then local c _ ret,r _ ret = pcall (rewrite.exec,uri) - c_ret表示成功執(zhí)行,r _ ret表示重定向,兩者都為真,表示成功重寫,所以如果c _ ret和r _ ret then返回end-url重寫不繼續(xù),然后在lua目錄中增加一個rewrite.lua文件,內(nèi)容如下。
rewrite.lua
local _ m = { } function _ m . exec(uri)local rewrite _ urls = { } local query string = ngx . var . args if query string = = nil thenquerystring = end rewrite _ urls[ ;/索引/文章 ]= ;/文章? ..query string local match _ url = rewrite _ urls[uri]if match _ url then-ngx . redirect(match _ url)-url change ngx . exec(match _ url)。- url不變return true end return failed journey _ m url不限于當前站點,可以跨域名。比如在一些非常常見的場景下,電腦端網(wǎng)頁在手機問時,可以調(diào)整到另一個域名或頁面,以便更好地在移動終端上顯示。舉個例子,
local agent = ngx . var . http _ user _ agent if agent ~ = nil then local m,ret = ngx.re.match(agent, android | webos | iphone | ipod |黑莓 )如果m ~ = nilten-重寫...同上,但外層多了一層判斷。判斷設備結(jié)束的示例代碼見demo10。
標簽:
重寫重定向
了解更多url重寫可以用什么接口(url重寫sessionid)?相關內(nèi)容請關注本站點。