Nginx下WordPress鏈接(url偽靜態(tài))301永久重定向?qū)崿F(xiàn)方法
問題:
在幾個blog程序中折騰的結(jié)果~
導(dǎo)致url連續(xù)二次變化。這是第三次了。
nginx 通過rewrite 使用 “permanent;”參數(shù) 成301永久url重定向。
以往的url結(jié)構(gòu)
http://chabaoo.cn/post/199/
現(xiàn)在需要的url結(jié)構(gòu)
http://chabaoo.cn/archives/199.html
過程:
學(xué)習(xí)nginx的配置規(guī)則,學(xué)習(xí)正則表達式(我也沒接觸過,學(xué)唄。)
nginx的中文維科:http://wiki.nginx.org/NginxChs
正則表達式入門:http://zh.wikipedia.org/zh-cn/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F
http://chabaoo.cn/tools/zhengze.html
簡單說明下(認真學(xué)習(xí)正則表達式-我沒認真學(xué)^_^):
^ 表示 匹配字符串的開始.
$ 表示 匹配字符串的結(jié)束.
$1 $2 表示變量
([0-9]+) 表示至少1個、最多不限制的數(shù)字串.
頭看暈了(不是程序員。)。寫出表達式。
—————————————————-
—————————————————-
<A class="st_tag internal_tag" title="標簽 rewrite 下的日志" rel=tag>rewrite</A> ^/post/([0-9]+)/$ /archives/$1.html permanent;
<A class="st_tag internal_tag" title="標簽 rewrite 下的日志" rel=tag>rewrite</A> ^/html/y2009/([0-9]+).html$ /archives/$1.html permanent;
把表達式加入nginx conf 文件。
—————————————————–
location / {
放這里。
}
—————————————————-
完成配置:
這是我的完整的WordPress nginx rewrite 規(guī)則配置。
1、nginx rewrite網(wǎng)址url變更301重定向。
2、nginx rewrite iamle.cn 重定向到 chabaoo.cn 。
3、nginx WordPress rewrite偽靜態(tài)規(guī)則(得以支持WordPress的自定義url)。
location / {
rewrite ^/post/([0-9]+)/$ /archives/$1.html permanent;
rewrite ^/html/y2009/([0-9]+).html$ /archives/$1.html permanent;
if ($host !~ "^www\.iamle\.com$"){
rewrite ^(.*) http://chabaoo.cn$1 permanent;
}
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
測試結(jié)果:
訪問:http://chabaoo.cn/post/199/ 即可被跳轉(zhuǎn)到 http://chabaoo.cn/archives/199.html
相關(guān)文章
Nginx圖片服務(wù)器配置之后圖片訪問404的問題解決
本文主要介紹了Nginx圖片服務(wù)器配置之后圖片訪問404的問題解決,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03Nginx解決Http慢攻擊(Slow HTTP Attack)的方法
緩慢的HTTP拒絕服務(wù)攻擊是一種專門針對于Web的應(yīng)用層拒絕服務(wù)攻擊,本文給大家介紹了Nginx解決Http慢攻擊(Slow HTTP Attack)的方法,需要的朋友可以參考下2024-02-02Apache和Nginx實現(xiàn)虛擬主機的3種方式小結(jié)
Apache是一個模型化的服務(wù)器,可以運行在幾乎所有的服務(wù)器上。其屬于應(yīng)用服務(wù)器,這篇文章主要介紹了Apache和Nginx實現(xiàn)虛擬主機的3種方式,需要的朋友可以參考下2023-11-11使用Kubernetes部署Springboot或Nginx的詳細教程
這篇文章主要介紹了用Kubernetes部署Springboot或Nginx的詳細教程,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07