Apache Nginx URL 地址 重寫

來源:互聯網
上載者:User

標籤:blog   bre   include   重寫   指定   pass   root   代碼   perm   

URL重寫這東西在工作中用了很多次了,但每次都忘記了要記得把知道的積累下來。

哎,要麼認為沒必要,要麼就是沒時間?!

 

一、Apache 篇

官方地址:http://man.chinaunix.net/newsoft/ApacheManual/mod/mod_rewrite.html

1.htaccess基本文法介紹

伺服器有設定檔不可能由我們來改,所以大多情況下要在網站的根目錄下建一個.htaccess檔案。

#設定重寫的根目錄
RewriteBase /
#開啟重寫引擎
RewriteEngine on

 

#RewriteCond 匹配所有合格請求

#如果不是檔案、不是目錄
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#執行RewriteRule規則體
#如果命中了這條,則不執行下面的所有重寫規則
RewriteRule ^/?front/ /front/index.html [L]
#下面兩條命中,都會被執行
RewriteRule ^/?front/abc /background/abc.html
RewriteRule ^/?front/edf /background/edf.html

 


#如果不是檔案、不是目錄
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
#執行RewriteRule規則體
RewriteRule . index.php [L]

其中RewriteCond 就是程式語言中的 if 邏輯, RewriteRule 代表的就是代碼體。[L] 代表的是break 。[NC] 標籤表示不區分大小寫

邏輯類似:

 

2、Apache mod_rewrite規則重寫的標誌一覽
1) R[=code](force redirect) 強制外部重新導向
強制在替代字串加上http://thishost[:thisport]/首碼重新導向到外部的URL.如果code不指定,將用預設的302 HTTP狀態代碼。
2) F(force URL to be forbidden)禁用URL,返回403HTTP狀態代碼。
3) G(force URL to be gone) 強制URL為GONE,返回410HTTP狀態代碼。
4) P(force proxy) 強制使用代理轉寄。
5) L(last rule) 表明當前規則是最後一條規則,停止分析以後規則的重寫。
6) N(next round) 重新從第一條規則開始運行重寫過程。
7) C(chained with next rule) 與下一條規則關聯
如果規則匹配則正常處理,該標誌無效,如果不匹配,那麼下面所有關聯的規則都跳過。
8) T=MIME-type(force MIME type) 強制MIME類型
9) NS (used only if no internal sub-request) 只用於不是內部子請求
10) NC(no case) 不區分大小寫
11) QSA(query string append) 追加請求字串
12) NE(no URI escaping of output) 不在輸出轉義特殊字元
例如:RewriteRule /foo/(.*) /bar?arg=P1%3d$1 [R,NE] 將能正確的將/foo/zoo轉換成/bar?arg=P1=zoo
13) PT(pass through to next handler) 傳遞給下一個處理
例如:

RewriteRule ^/abc(.*) /def$1 [PT] # 將會交給/def規則處理
Alias /def /ghi

14) S=num(skip next rule(s)) 跳過num條規則
15) E=VAR:VAL(set environment variable) 設定環境變數


3、Apache 內建變數說明

http://blog.sina.com.cn/s/blog_6bad64c20101a3n9.html

4、Apache 使用樣本

http://www.jb51.net/article/47907.htm

 

 

二、Nginx 篇

官方地址:http://nginx.org/en/docs/http/ngx_http_rewrite_module.html

在nginx的[ /data/servers/nginx/conf/domains/www.demo.com.conf ]配置中添加如下內容,使支援.htaccess_nginx檔案。

include /home/wwwroot/www.demo.com/.htaccess_nginx;
1.htaccess_nginx 基本文法介紹

.htaccess_nginx 檔案內容

if ($request_filename ~ "^.*.(forum\.php).*$"){  rewrite ^/forum.php/(.*)$ /forum.php?s=/$1 last;  break;}#並且條件set $flag 0;if (!-e $request_filename) {  set $flag "${flag}1";}if ($request_filename ~ "^.*.(/front/).*$") {  set $flag "${flag}2";}if ($flag = "012") {  rewrite ^/front/(.*)$ /front/index.html last;  break;}if (!-e $request_filename){  rewrite ^/(.*)$ /index.php last;}

詳細見:http://www.nginx.cn/216.html

 

2、Nginx rewrite規則重寫的標誌一覽

1) last 停止處理後續rewrite指令集,然後對當前重寫的新URI在rewrite指令集上重新尋找。
2) break 停止處理後續rewrite指令集,並不在重新尋找,但是當前location內剩餘非rewrite語句和location外的的非rewrite語句可以執行。
3) redirect 如果replacement不是以http:// 或https://開始,返回302臨時重新導向
4) permant 返回301永久重新導向

3、Nginx 內建變數說明

http://www.cnphp.info/nginx-embedded-variables-lasted-version.html

4、Nginx 使用樣本

http://blog.csdn.net/wave_1102/article/details/46483897

 

 

三、Apache 和 Nginx 對比

兩種伺服器實現的同一效果的URL重寫邏輯對比圖:

 

相同點:

都使用通用的正則文法做URL匹配。

不同點:

ngingx 無法使用並且語句。需另闢蹊徑,使用set 語句來處理。

對應的標籤和文法關鍵字不同。

 

 

 

 

 

ps:

http://man.chinaunix.net/newsoft/ApacheManual/mod/mod_rewrite.html

http://www.runoob.com/regexp/regexp-syntax.html

http://www.cnblogs.com/yuanzai12345/p/5976090.html

 

Apache Nginx URL 地址 重寫

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.