1. 程式人生 > >Apache Nginx URL 地址 重寫

Apache Nginx URL 地址 重寫

man tro 內置 span ont 文件內容 http狀態碼 規則 lac

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 地址 重寫