一、什么是偽靜態
偽靜態即是網站本身是動態網頁如.php、.asp、.aspx等格式動態網頁有時這類動態網頁還跟"?"加參數來讀取數據庫內不同資料,偽靜態就是做url重寫操作(即rewrite)。很典型的案例即是discuz論壇系統,后台就有一個設置偽靜態功能,開啟偽靜態后,動態網頁即被轉換重寫成靜態網頁類型頁面,通過瀏覽器訪問地址和真的靜態頁面沒區別。但是記住:做偽靜態的前提就是服務器要支持偽靜態重寫URL Rewrite功能。
考慮搜索引擎優化(即SEO),將動態網頁通過服務器處理成靜態頁面,如www.kevin.com/jk/fd.php?=12這樣的動態網頁處理成www.kevin.com/jk-fd-12.html這樣格式靜態頁面,常見的論壇帖子頁面,都是經過偽靜態處理成靜態頁面格式html頁面。由於網站所用的程序語言不易被發現,經過重寫來偽靜態來將動態網頁的程序后綴變為html的靜態頁面格式。偽靜態是一種可以把文件后綴改成任何可能的一種方法,比如如果想把php文件偽靜態成html文件,這種配置相當簡單的,后面會提到相應配置。
二、真靜態、偽靜態優點和缺點
真靜態(html)優點;1)減少服務器對數據響應的負荷;2)加載不用調動數據庫,響應速度快。
真靜態缺點:1)維護不方便,每次都要手動生成;2)空間占用比較大,容易造成磁盤壓力;3)生成的文件多,服務器對html文件的響應負擔也較重。
偽靜態(url重寫)優點:1)可以方便的實現對化化引擎的優化,並且比生成靜態更加方便;2)占空間比較小;3)首頁每天都自動變化,不用維護。網站首頁一般都有熱點排行之類的,你可以設為,24小時排行,一周排行,再加上最新文章,最新點評等。這樣首頁天天是有變化的;4)便於廣告的輪顯。比如:你可以把 art1234.aspx,這個虛成n個頁,如art_1234.aspx,news_1234.aspx,top_1234.aspx,在不同的頁面放 不同的廣告。總之是動態的,就可以隨意動。
偽靜態缺點:1)如果流量稍大一些使用偽靜態就出現CPU使用超負荷,因為偽靜態是用正則判斷而不是真實地址,分辨到底顯示哪個頁面的責任也由直接指定轉由CPU來判斷了,所以CPU占有量的上升,確實是偽靜態最大的弊病;2)偽靜態效率不如生成html的,因為它不是真正意義上的靜態頁,所以每次請求都是要去讀取數據庫的信息(這個可以用緩存技術來補償一下)。
三、真靜態、偽靜態原理與實現方案
1. 偽靜態
偽靜態是相對於真靜態而言的,就是把一些asp,php等結尾url通過apche或nginx的重寫規則,變成以html一類的靜態頁面形式。偽靜態不是真正的靜態,它和動態地址一樣要讀取數據庫。偽靜態最主要的作用就是利於seo,百度spider(百度蜘蛛)喜歡抓取靜態頁面,可容易使百度spider陷入死循環;並發量高的時候會加大服務器的壓力,所以用的時候要注意。
偽靜態就是利用apche,nginx重寫規則,對url地址重寫實現的!偽靜態實現原理:
1) Apache偽靜態前提是要打開apache的重寫模塊 (即打開"LoadModule rewrite_module modules/mod_rewrite.so"這一行);
2) Nginx默認就支持偽靜態;
偽靜態有兩種配置方式
1) 在配置虛擬主機的時候設置;
2) 在web根目錄下創建一個.htaccess文件,在這個文件里面配置;
2. 真靜態
在網站設計中,純粹HTML(標准通用標記語言下的一個應用)格式的網頁通常被稱為"靜態網頁",靜態網頁是標准的HTML文件,它的文件擴展名是.htm、.html,可以包含文本、圖像、聲音、FLASH動畫、客戶端腳本和ActiveX控件及JAVA小程序等。靜態網頁是網站建設的基礎,早期的網站一般都是由靜態網頁制作的。靜態網頁是相對於動態網頁而言,是指沒有后台數據庫、不含程序和不可交互的網頁。靜態網頁相對更新起來比較麻煩,適用於一般更新較少的展示型網站。容易誤解的是靜態頁面都是htm這類頁面,實際上靜態也不是完全靜態,它也可以出現各種動態的效果,如GIF格式的動畫、FLASH、滾動字幕等。
大型web項目優化中經常會考慮到使用真靜態,這樣在訪問量大的時候,可以減少cpu的壓力,但是會生成大量的文件占用網站的磁盤空間,可以寫個php的腳本或用linux的計划任務進行刪除。在用真靜態的時候有的時候需要用到局部的動態化。
真靜態實現方法
1)利用PHP模板生成靜態頁面;
2)使用PHP文件讀寫功能生成靜態頁面;
3)使用PHP輸出控制函數緩存機制生成靜態頁面;
4)使用nosql從內存中讀取內容(其實這個已經不算靜態化了而是緩存);
memcached是鍵值一一對應,key默認最大不能超過128個字節,value默認大小是1M,因此1M大小滿足大多數網頁大小的存儲。
真靜態和偽靜態的區別
1)是不是一個真正靜態頁面;
2)有沒有和數據庫或后台程序進行交互;
3)它們的應用場景和解決的問題不同;
4)用javascript:alert(document.lastModified)來判斷是真靜態還是偽靜態;
真靜態在apache和nginx上的區別與否
1)真靜態在nginx上的運行速度比apache運行速度快;
2)nginx處理靜態文件對於apache來說消耗的內存少;
偽靜態在apache和nginx上的區別與否
1)本質上沒有區別,兩者都是根據正則匹配對應的url的重寫。但是apache和nginx上的偽靜態規則還是有點不同,在配置的時候要注意;
2)apache處理偽靜態比nginx更有優勢;
3. 簡單小示例
跳轉需求: 訪問http://www.kevin.com/p/123456.html 跳轉成 http://a.aa.com/p/123456 配置如下: rewrite ^/p/(\d+).html http://www.kevin.com/p/$1 last; 解釋說明: \d是數字的意思 +是最少一個{1,} 1到無窮大{1,3} 這樣是1-3位數。
四、Nginx偽靜態配置和常用Rewrite偽靜態規則
1. nginx防盜鏈
location ~* \.(gif|jpg|png)$ { valid_referers none blocked http://kevin.com:81; #允許訪問的來源,即所有來自http://kevin.com:81的gif|jpg|png結尾的文件 if ($invalid_referer) { rewrite ^/ http://kevin.com:81/c2_.html; #return 403; } }
2. 偽靜態重寫
location / { rewrite c(\d+)_(.*).html /index.php?c=user&m=index&id=$1&title=$2 last; root /usr/share/nginx/html/sta; index index.html index.htm index.php; }
******** 對於生成大量的大量html文件,推薦使用rsync工具進行批量刪除
1) 建立一個空目錄 # mkdir -p /root/kevin/tmp/ 2) 確認需要清空的目標目錄(即需要刪除的大量html文件所在的目錄),比如/root/kevin/tmp1/ 3)使用rsync同步刪除(注意目錄后面的“/”),整體效率會快一個數量級的樣子。 # rsync --delete-before -a -H /root/kevin/tmp/ /root/kevin/tmp1/ 選項說明: –delete-before 接收者在傳輸之前進行刪除操作 –progress 在傳輸時顯示傳輸過程 -a 歸檔模式,表示以遞歸方式傳輸文件,並保持所有文件屬性 -H 保持硬連接的文件 -v 詳細輸出模式 -stats 給出某些文件的傳輸狀態 如下操作可以把某個超大文件刪掉,比如刪除/mnt/ops/web.html文件 1)建立空文件/mnt/ops/html.txt 2)# rsync --delete-before -a -H -v --progress --stats /mnt/ops/html.txt /mnt/ops/web.html 3)這樣置為空后就可以快速刪掉 原理: 把文件系統的目錄與書籍的目錄做類比,rm刪除內容時,將目錄的每一個條目逐個刪除(unlink),需要循環重復操作很多次; rsync刪除內容時,建立好新的空目錄,替換掉老目錄,基本沒開銷。 測試大文件刪除 1)生成文件 # for i in $(seq 1 500000); do echo test >>/opt/test/$i.txt; done 測試結果: 10萬文件rm刪除第一次11s左右,第二次9s左右;rsync測試兩次9s左右 50萬文件rm刪除報錯;rsync測試兩次一個5分左右,一個4分左右 rm刪除命令 # time rm -f /opt/test/*.txt rsync命令刪除 # mkdir /opt/test1 # rsync --delete-before -a -H -v --progress --stats /opt/test1/ /opt/test/
******** nginx php thinkphp5 偽靜態配置示例
server { listen 80; server_name 127.0.0.1 localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { if (!-e $request_filename) { #一級目錄 #rewrite ^/(.*)$ /index.php?s=$1 last; #二級目錄 rewrite ^/TWcloud/public/(.*)$ /TWcloud/public/index.php?s=$1 last; break; } root /Applications/MxSrvs/www; index index.html index.htm index.php; } location ~ \.php { #去掉$ root /Applications/MxSrvs/www; fastcgi_pass 127.0.0.1:10080; fastcgi_index index.php; fastcgi_split_path_info ^(.+\.php)(.*)$; #增加這一句 fastcgi_param PATH_INFO $fastcgi_path_info; #增加這一句 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
******** thinkphp 5.0的nginx偽靜態規則配置示例 [踩過坑點,經測試實現]
server { listen 80; server_name www.kevin.com; root /data/www/web; location / { index index.html index.htm index.php default.php; #重點就是加入下面這個if if (!-e $request_filename){ rewrite ^(.*)$ /index.php?s=$1 last; break; } } location ~ .php(.*)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.html; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_split_path_info ^(.+.php)(.*)$; fastcgi_param PATH_INFO $fastcgi_path_info; include fastcgi_params; } }
******** 先來看一個nginx偽靜態配置示例
場景一: 把 http://www.abc.com/index.php/front/index/index 重寫成 http://www.abc.com/a.html 場景二: 把下面帶參數的第1、2個url解析成第3個url 1.http://www.abc.com/index.php/front/index/parse/name/yangxignyi/age/18 2.http://www.abc.com/index.php/front/index/parse?name=yangxignyi&age=18 3.http://www.abc.com/parse-yangxignyi-18.html 服務器配置文件: server{ listen 80; server_name www.abc.com; root /data/www/web; location / { index index.php index.htm /public/index.html; autoindex off; include abc.conf; #rewrite a.html /index.php/front/index/index last; } location ~ \.php(.*)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^((?U).+\.php)(/?.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; include fastcgi_params; } 偽靜態配置文件可以直接寫在location /{} 里面的,不推薦這樣做!! 建議新增加個rewrite.conf文件,寫偽靜態文件會好點,include 引入進來就行了,這樣可以在rewrite.conf里面寫n多配置 location / { index index.php index.htm /public/index.html; autoindex off; include rewrite.conf; #rewrite a.html /index.php/front/index/index last; } #rewrite.conf (這個文件自己創建就行了,文件內容寫規則),偽靜態配置如下: #場景一的規則 #http://www.abc.com/index.php/front/index/index rewrite a.html /index.php/front/index/index last; #場景二的規則 #1.http://www.abc.com/index.php/front/index/parse/name/yangxignyi/age/18 #2.http://www.abc.com/index.php/front/index/parse?name=yangxignyi&age=18 #3.http://www.abc.com/parse-yangxingyi-18.html rewrite parse-(\w+)-(\d+).html /index.php/front/index/parse/name/$1/age/$2 last; 如上配置中的\w是數字字母下划線的意思,\d是數字的意思 +是最少一個{1,} 1到無窮大{1,3} 這樣是1-3位數。
3. nginx偽靜態的Rewrite重寫的正則匹配規則
正則表達式匹配 : ~ 為區分大小寫的匹配 ~* 不區分大小寫的匹配(匹配firefox的正則同時匹配FireFox) !~ 區分大小寫的不匹配 !~* 不區分大小寫的不匹配 \ 將后面接着的字符標記為一個特殊字符或者一個原義字符或一個向后引用 . 匹配除換行符以外的任意字符,即匹配除"\n"之外的所有單個字符 \w 匹配字母或數字或下划線或漢字 \s 匹配任意的空白符 \d 匹配數字 \b 匹配單詞的開始或結束 ^ 匹配字符串的開始,即匹配輸入字符串的起始位置 $ 匹配字符串的結束,即匹配輸入字符串的結束位置 * 重復零次或更多次,即匹配前面的字符零次或者多次 + 重復一次或更多次,即匹配前面字符串一次或者多次 ? 重復零次或一次,即匹配前面字符串的零次或者一次 {n} 重復n次 {n,} 重復n次或更多次 {n,m} 重復n到m次 *? 重復任意次,但盡可能少重復 +? 重復1次或更多次,但盡可能少重復 ?? 重復0次或1次,但盡可能少重復 {n,m}? 重復n到m次,但盡可能少重復 {n,}? 重復n次以上,但盡可能少重復 \W 匹配任意不是字母,數字,下划線,漢字的字符 \S 匹配任意不是空白符的字符 \D 匹配任意非數字的字符 \B 匹配不是單詞開頭或結束的位置 [^x] 匹配除了x以外的任意字符 文件及目錄匹配,其中: -f和!-f 用來判斷是否存在文件 -d和!-d 用來判斷是否存在目錄 -e和!-e 用來判斷是否存在文件或目錄 -x和!-x 用來判斷文件是否可執行 flag標記有: last 相當於Apache里的[L]標記,表示完成rewrite。即本條規則匹配完成后繼續向下匹配新的location URI規則 break 終止匹配, 不再匹配后面的規則。即本條規則匹配完成后終止,不在匹配任何規則 redirect 返回302臨時重定向 地址欄會顯示跳轉后的地址 permanent 返回301永久重定向 地址欄會顯示跳轉后的地址 $args 此變量與請求行中的參數相等 $content_length 等於請求行的“Content_Length”的值。 $content_type 等同與請求頭部的”Content_Type”的值 $document_root 等同於當前請求的root指令指定的值 $document_uri 與 $uri 一樣 $host 與請求頭部中“Host”行指定的值或是request到達的server的名字(沒有Host行)一樣 $limit_rate 允許限制的連接速率 $request_method 等同於request的method,通常是“GET”或“POST” $remote_addr 客戶端ip $remote_port 客戶端port $remote_user 等同於用戶名,由ngx_http_auth_basic_module認證 $request_filename 當前請求的文件的路徑名,由root或alias和URI request組合而成 $request_body_file $request_uri 含有參數的完整的初始URI $query_string 與 $args一樣 $server_protocol 等同於request的協議,使用“HTTP/1.0”或“HTTP/1.1” $server_addr request 到達的server的ip,一般獲得此變量的值的目的是進行系統調用。為了避免系統調用,有必要在listen指令中指明ip,並使用bind參數。 $server_name 請求到達的服務器名 $server_port 請求到達的服務器的端口號 $uri 等同於當前request中的URI,可不同於初始值,例如內部重定向時或使用index
總的來說,location中的rewrite,不寫last和break,那么流程就是依次執行這些rewrite
1. rewrite btrak:表示url重寫后,直接使用當前資源,不再執行location里余下的語句,完成本次請求后,地址url不變。
2. rewrite last:表示url重寫后,馬上發起一個新的請求,再次進入server塊,重試location匹配,超過10次匹配不到就報500錯誤,地址欄url不變。
3. rewrite redirect:表示返回302臨時重定向,地址欄顯示重定向后的url,爬蟲不會重寫url,因為是臨時。
4. rewrite permanent:表示返回301永久重定向,地址欄顯示重定向后的url,爬蟲重寫url。
rewrite重寫跳轉的應用場景
1. 調整用戶瀏覽的URL,看起來規范
2. 為了讓搜索引擎收錄網站內容,讓用戶體驗更好
3. 網站更換新域名后
4. 根據特殊的變量、目錄、客戶端信息進行跳轉
******** 常用301跳轉的rewrite重寫示例
1)301跳轉 之前常用起別名的方式做到了不同地址訪問同一個虛擬主機的資源,現在可以用一個更好的方式做到這一點,那就是跳轉的方法來實現。 比如www.kevin.com虛擬主機為例子,修改配置文件如下: server { # 添加個server區塊做跳轉 listen 80; server_name kevin.com; rewrite ^/(.*) http://www.kevin.com/$1 permanent; } server { listen 80; server_name www.kevin.com; location / { root html/brian; index index.html index.htm; } access_log logs/brian.log main gzip buffer=128k flush=5s; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } 這樣,當訪問域名http://kevin.com時,就301永久跳轉到http://www.kevin.com域名了,跳轉后的地址是http://www.kevin.com 2)域名跳轉: 不僅可以做相同虛擬主機的資源域名跳轉,也能做不同虛擬主機的域名跳轉,下面就跳轉下當訪問kevin.com域名的時候跳轉到www.baidu.com的頁面: 修改配置文件: server { #添加個server區塊做跳轉 listen 80; server_name kevin.com; rewrite ^/(.*) http://www.kevin.com/$1 permanent; } server { listen 80; server_name www.kevin.com; location / { root html/brian; index index.html index.htm; } access_log logs/brian.log main gzip buffer=128k flush=5s; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } 這樣,當訪問http://kevin.com時,就跳轉到了http://www.baidu.com,跳轉后的地址是http://www.baidu.com
4. nginx偽靜態常用配置示例
nginx里使用偽靜態是直接在nginx.conf 中寫規則的,並不需要像apache要開啟寫模塊(mod_rewrite)才能進行偽靜態。 nginx只需要打開nginx.conf配置文件,在server里面寫需要的規則即可。 1)配置案例1 server { listen 80; server_name www.kevin.com; index index.html index.htm index.php; rewrite ^/wangla.html$ http://www.baidu.com/ permanent; rewrite ^/(\d+).html$ http://www.qq.com/ permanent; rewrite ^/(\w+).html$ http://wd.gyyx.cn/index_wd_v5.htm permanent; } 以上添加了幾條重寫規則 訪問www.kevin.com/wangla.html跳轉到百度 訪問www.kevin.com/純數字至少一個數字.html跳轉到QQ官網 訪問www.kevin.com/匹配字母或數字或下划線組合.html 跳轉到問道官網。 2)配置案例2 server { listen 80; server_name bbs.jb51.net; index index.html index.htm index.php; root /home/www/bbs; error_page 404 /404.htm; #配置404錯誤頁面 location ~ .*.(php|php5)?$ { #fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fcgi.conf; } #下面就是偽靜態了 location /{ rewrite ^(.*)/equip(d+).html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last; } access_log access_log off; } 然后重啟nginx服務器偽靜態就生效了,這種維護起來很是不方便我們可以把它寫在外部文件如bbs_nginx.conf中 在/home/www/bbs目錄下創建bbs_nginx.conf文件並寫入以下代碼: location /{ rewrite ^(.*)/equip(d+).html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last; } 然后在上面的代碼后面加上如下代碼: include /home/www/bbs/bbs_nginx.conf; 這樣網站根目錄中的bbs_nginx.conf偽靜態規則,即可實現單獨管理。 下面是一個實例: 在使用.htaccess文件的目錄下新建一個.htaccess文件,如下面一個Discuz論壇目錄: # vim /var/www/html/jb51/bbs/.htaccess rewrite ^(.*)/archiver/((fid|tid)-[w-]+.html)$ $1/archiver/index.php?$2 last; rewrite ^(.*)/forum-([0-9]+)-([0-9]+).html$ $1/forumdisplay.php?fid=$2&page=$3 last; rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ $1/viewthread.php?tid=$2&extra=page%3D$4&page=$3 last; rewrite ^(.*)/profile-(username|uid)-(.+).html$ $1/viewpro.php?$2=$3 last; rewrite ^(.*)/space-(username|uid)-(.+).html$ $1/space.php?$2=$3 last; rewrite ^(.*)/tag-(.+).html$ $1/tag.php?name=$2 last; 接着修改nginx配置文件: # vim /etc/nginx/nginx.conf 在需要添加偽靜態的虛擬主機的server{}中引入.htaccess文件: include /var/www/html/jb51/bbs/.htaccess; 最后重新加載nginx配置文件: # /etc/init.d/nginx reload 3)下面是一些rewrite配置集錦,供運維參考: ======================================================================================= rewrite "^/(.{6})(\d{3})(.+)/php/" http://www.kevin.com/qq$2.apk break; 中間用到了{6}指前面的字符得復6次 結合PHP的例子 if (!-d $request_filename) { rewrite ^/([a-z-A-Z]+)/([a-z-A-Z]+)/?(.*)$ /index.php?namespace=user&controller=$1&action=$2&$3 last; rewrite ^/([a-z-A-Z]+)/?$ /index.php?namespace=user&controller=$1 last; break; 多目錄轉成參數。 abc.domian.com/sort/2 => abc.domian.com/index.php?act=sort&name=abc&id=2 配置如下: if ($host ~* (.*)\.domain\.com) { set $sub_name $1; rewrite ^/sort\/(\d+)\/?$ /index.php?act=sort&cid=$sub_name&id=$1 last; } 目錄對換 /123456/xxxx -> /xxxx?id=123456 配置如下: rewrite ^/(\d+)/(.+)/ /$2?id=$1 last; 例如下面設定nginx在用戶使用ie的使用重定向到/nginx-ie目錄下: if ($http_user_agent ~ MSIE) { rewrite ^(.*)$ /nginx-ie/$1 break; } 目錄自動加"/" if (-d $request_filename){ rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent; } 禁止htaccess location ~/\.ht { deny all; } 禁止多個目錄 location ~ ^/(cron|templates)/ { deny all; break; } 禁止以/data開頭的文件 可以禁止/data/下多級目錄下.log.txt等請求; location ~ ^/data { deny all; } 禁止單個目錄 不能禁止.log.txt能請求 location /searchword/cron/ { deny all; } 禁止單個文件 location ~ /data/sql/data.sql { deny all; } 給favicon.ico和robots.txt設置過期時間; 這里為favicon.ico為99 天,robots.txt為7天並不記錄404錯誤日志 location ~(favicon.ico) { log_not_found off; expires 99d; break; } location ~(robots.txt) { log_not_found off; expires 7d; break; } 設定某個文件的過期時間;這里為600秒,並不記錄訪問日志 location ^~ /html/scripts/loadhead_1.js { access_log off; root /opt/lampp/htdocs/web; expires 600; break; } 文件反盜鏈並設置過期時間 這里的return 412 為自定義的http狀態碼,默認為403,方便找出正確的盜鏈的請求 "rewrite ^/ http://leech.c1gstudio.com/leech.gif;"顯示一張防盜鏈圖片 "access_log off;"不記錄訪問日志,減輕壓力 "expires 3d"所有文件3天的瀏覽器緩存 location ~* ^.+\.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ { valid_referers none blocked *.c1gstudio.com *.c1gstudio.net localhost 208.97.167.194; if ($invalid_referer) { rewrite ^/ http://leech.c1gstudio.com/leech.gif; return 412; break; } access_log off; root /opt/lampp/htdocs/web; expires 3d; break; } 只充許固定ip訪問網站,並加上密碼 root /opt/htdocs/www; allow 218.197.16.14; allow 22.133.11.22; allow 21.112.69.14; deny all; auth_basic "C1G_ADMIN"; auth_basic_user_file htpasswd; 將多級目錄下的文件轉成一個文件,增強seo效果 /job-123-456-789.html 指向/job/123/456/789.html 配置如下: rewrite ^/job-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /job/$1/$2/jobshow_$3.html last; -------------------------------------------------------------------------------- 將根目錄下某個文件夾指向2級目錄 如/shanghaijob/ 指向 /area/shanghai/ 如果你將last改成permanent,那么瀏覽器地址欄顯是 /location/shanghai/ 配置如下: rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last; 上面例子有個問題是訪問/shanghai 時將不會匹配 rewrite ^/([0-9a-z]+)job$ /area/$1/ last; rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last; 這樣/shanghai 也可以訪問了,但頁面中的相對鏈接無法使用, 如./list_1.html真實地址是/area /shanghia/list_1.html會變成/list_1.html,導至無法訪問。 那加上自動跳轉也是不行的! (-d $request_filename)它有個條件是必需為真實目錄,而我的rewrite不是的,所以沒有效果。 if (-d $request_filename){ rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent; } 知道原因后就好辦了,手動跳轉吧 rewrite ^/([0-9a-z]+)job$ /$1job/ permanent; rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last; -------------------------------------------------------------------------------- 文件和目錄不存在的時候重定向: if (!-e $request_filename) { proxy_pass http://127.0.0.1/; } 域名跳轉 server { listen 80; server_name jump.c1gstudio.com; index index.html index.htm index.php; root /opt/lampp/htdocs/www; rewrite ^/ http://www.c1gstudio.com/; access_log off; } 多域名轉向 server_name http://www.c1gstudio.com/ http://www.c1gstudio.net/; index index.html index.htm index.php; root /opt/lampp/htdocs; if ($host ~ "c1gstudio\.net") { rewrite ^(.*) http://www.c1gstudio.com$1/ permanent; } 三級域名跳轉 if ($http_host ~* "^(.*)\.i\.c1gstudio\.com$") { rewrite ^(.*) http://top.yingjiesheng.com$1/; break; } 域名鏡向 server { listen 80; server_name mirror.c1gstudio.com; index index.html index.htm index.php; root /opt/lampp/htdocs/www; rewrite ^/(.*) http://www.c1gstudio.com/$1 last; access_log off; } 某個子目錄作鏡向 location ^~ /zhaopinhui { rewrite ^.+ http://zph.c1gstudio.com/ last; break; } discuz ucenter home (uchome) rewrite偽靜態配置 rewrite ^/(space|network)-(.+)\.html$ /$1.php?rewrite=$2 last; rewrite ^/(space|network)\.html$ /$1.php last; rewrite ^/([0-9]+)$ /space.php?uid=$1 last; discuz 7 rewrite偽靜態配置 rewrite ^(.*)/archiver/((fid|tid)-[\w\-]+\.html)$ $1/archiver/index.php?$2 last; rewrite ^(.*)/forum-([0-9]+)-([0-9]+)\.html$ $1/forumdisplay.php?fid=$2&page=$3 last; rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/viewthread.php?tid=$2&extra=page\=$4&page=$3 last; rewrite ^(.*)/profile-(username|uid)-(.+)\.html$ $1/viewpro.php?$2=$3 last; rewrite ^(.*)/space-(username|uid)-(.+)\.html$ $1/space.php?$2=$3 last; rewrite ^(.*)/tag-(.+)\.html$ $1/tag.php?name=$2 last; 給discuz某版塊單獨配置域名 server_name bbs.c1gstudio.com news.c1gstudio.com; location = / { if ($http_host ~ news\.c1gstudio.com$) { rewrite ^.+ http://news.c1gstudio.com/forum-831-1.html last; break; } } discuz ucenter 頭像 rewrite 優化 location ^~ /ucenter { location ~ .*\.php?$ { #fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fcgi.conf; } location /ucenter/data/avatar { log_not_found off; access_log off; location ~ /(.*)_big\.jpg$ { error_page 404 /ucenter/images/noavatar_big.gif; } location ~ /(.*)_middle\.jpg$ { error_page 404 /ucenter/images/noavatar_middle.gif; } location ~ /(.*)_small\.jpg$ { error_page 404 /ucenter/images/noavatar_small.gif; } expires 300; break; } } jspace rewrite偽靜態配置 location ~ .*\.php?$ { #fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fcgi.conf; } location ~* ^/index.php/ { rewrite ^/index.php/(.*) /index.php?$1 break; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fcgi.conf; }
Nginx偽靜態配置示例2
公司域名bo.kevin.com下有多個子項目目錄結構大致是bo.kevin.com/own/xys |bo.kevin.com/2017/abc |bo.kevin.com/2018/def有二級也有三級目錄, 應開發需求某項目訪問地址是:bo.kevin.com/own/xys/index.php/admin/login 需要把index.php隱藏為bo.kevin.com/own/xys/index/admin/login進行訪問。 設定以下三種場景: 場景一 將 http://www.abc.com/index.php/front/index/index 重寫成 http://www.abc.com/a.html 場景二 將 http://www.abc.com/index.php/front/index/parse?name=itboy&age=18 重寫成 http://www.abc.com/parse-itboy-18.html 場景三(同一域名下,需要匹配隨時新增的二三級目錄,並隱藏index.php的.php后綴) 將 http://bo.kevin.com/own/xys/index.php/admin/login 以及 http://bo.kevin.com/2018/gdhp/index.php/login 重寫成 http://bo.kevin.com/own/xys/index/admin/login 以及 http://bo.kevin.com/2018/gdhp 建議在nginx/conf目錄下新建rewrite.conf配置文件中編寫偽靜態規則,寫完后在域名.conf文件中插入rewrite.conf文件即可(用include rewrite.conf插入)。 nginx配置文件如下: server { listen 80; server_name bo.kevin.com; index index.html index.php index.htm index.php default.html default.htm default.php; root /var/www/apps/bo.kevin.com; include rewrite.conf; include none.conf; error_page 502 /502.html; include enable-php-pathinfo.conf; location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 12h; } location ~ /\. { deny all; } access_log /var/www/wwwlogs/bo.kevin.com.log; error_log /var/www/wwwlogs/error.bo.kevin.com.log; } rewrite.conf文件內容如下: location /{ #場景一 #http://www.abc.com/index.php/front/index/index 變成 http://www.abc.com/a.html rewrite a.html /index.php/front/index/index last; #場景二 #http://www.abc.com/index.php/front/index/parse?name=itboy&age=18 變成 http://www.abc.com/parse-itboy-18.html rewrite parse-(\w+)-(\d+).html /index.php/front/index/parse/name/$1/age/$2 last; #場景三 #http://bo.kevin.com/own/xys/index.php/admin/login 以及 http://bo.kevin.com/2018/gdhp/index.php/login #變成 http://bo.kevin.com/own/xys/index/admin/login 以及 http://bo.kevin.com/2018/gdhp/index/login rewrite ^/(\w+)/(\w+)/(.*)$ /$1/$2/index.php?s=$3 last; #針對own目錄偽靜態規則,$1對應(\w+)部分,$2對應第二個(\w+)部分,$3對應(.*)部分,$代表直至最后 rewrite ^/(\d+)/(\w+)/(.*)$ /$1/$2/index.php?s=$3 last; #針對后期的2018下的子項目偽靜態規則 } 說明: 其實都是很簡單的對號入座原理而已,拿場景二來說明,第一個正則(\w+)對應的就是$1,第二個正則(\d+)對應的就是$2, 另外,\w是數字字母下划線的意思,\d是數字的意思 +是最少一個{1,} 1到無窮大{1,3} 這樣是1-3位數。
Nginx上支持.htaccess偽靜態的配置示例
# vim /usr/local/nginx/conf/vhost/web.conf ........ include /var/www/web/.htaccess # vim /var/www/web/.htaccess rewrite ^/show-([0-9]+)-([0-9]+)\.html$ /index.php?action=show&id=$1&page=$2; rewrite ^/category-([0-9]+)-([0-9]+)\.html$ /index.php?action=index&cid=$1&page=$2; rewrite ^/archives-([0-9]+)-([0-9]+)\.html$ /index.php?action=index&setdate=$1&page=$2; rewrite ^/(archives|search|reg|login|index|links)\.html$ /index.php?action=$1; rewrite ^/(comments|tagslist|trackbacks|index)-([0-9]+)\.html$ /index.php?action=$1&page=$2; if ($host != 'www.shibo.com' ) { rewrite ^/(.*)$ http://www.shibo.com/$1 permanent; } error_page 404 http://www.shibo.com/; # /usr/local/nginx/sbin/nginx -s reload
nginx偽靜態配置示例3 [去除框架的Index.php]
在nginx.conf中Location/{}中加上下面這個if判斷就可以了: location /{ if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=$1 last; break; } } =============================================================================== 如下面一個配置 if (!-e $request_filename) { rewrite ^/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)\.html /index.php?m=$1&c=$2&a=$3&$4=$5&$6=$7 last; break; }
Nginx 偽靜態配置示例4
訪問 http://www.kevin.com/sort/15.html -> http://www.kevin.com/1.php?id=15 location / { root /usr/share/nginx/html/1; index index.html; rewrite /sort/(.*)\.html /1.php?id=$1 last; } ================================================================================ 再如下面一個配置 server { listen 80 default_server; server_name _; location / { root /usr/share/nginx/html; index index.html index.htm; rewrite ^(.*)list-([0-9]+)-([0-9]+)\.html$ $1list.php?page=$2&id=$3; } } 策略:RewriteRule ^(.*)list-([0-9]+)-([0-9]+)\.html$ $1list.php?page=$2&id=$3 請求路徑:http://www.abc.com/list-123-456.html 以上策略分成兩段: 第一段是使用正則表達式去匹配請求訪問的路徑,第二段是將匹配后的參數轉化為真實訪問的路徑。 策略執行時:^(.*)list-([0-9]+)-([0-9]+)\.html$ 與 /list-123-456.html 這個字符串進行匹配: ^和$字符分別代表了匹配輸入字符串的開始和結束; ()中的匹配到的內容會被按順序分配到變量$1 $2 $3中; .*匹配任意字符串,且長度從0個到多個,故$1值為/; [0-9]+匹配字符0-9,長度1個到多個,故$2和$3分別是123和456; 所以最后真實訪問的動態地址為 /list.php?page=123&id=456 ================================================================================ 再如下面一個配置 1)/a/b?c=d => index.php?_a=a&_m=b&c=d 2)/xxx/detail-yyy.html => index.php?_a=xxx&_m=detail&id=yyy 配置如下: server { listen 80; server_name my.xh5.com; location / { root /mnt/hgfs/web/my.xueh5.com/src/; index index.html index.htm index.php; if ($args ~ "^(.*)$"){ set $rule_0 1$rule_0; set $bref_1 $1; } if ($rule_0 = "1"){ rewrite ^([0-9a-zA-Z]*)/detail-([0-9]*)\.html$ /index.php?_a=$1&_m=detail&id=$2&$bref_1 last; rewrite ^/([0-9a-zA-Z]*)/([0-9a-zA-Z.]*)$ /index.php?_a=$1&_m=$2&$bref_1 last; rewrite ^/([0-9a-zA-Z]+)$ /index.php?_a=$1&_m=index&$bref_1 last; rewrite ^/$ /index.php?_a=index&_m=index&$bref_1 last; } } location ~ \.php$ { root /mnt/hgfs/web/my.xueh5.com/src/; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
5. Nginx偽靜態常用規則配置總結
1)WordPress偽靜態 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; } 2)PHPCMS偽靜態 rewrite ^/caipu-([0-9]+)-([0-9]+)-([0-9]+).html /index.php?m=content&c=index&a=show&catid=$1&id=$2&page=$3 last; rewrite ^/content-([0-9]+)-([0-9]+)-([0-9]+).html /index.php?m=content&c=index&a=show&catid=$1&id=$2&page=$3 last; rewrite ^/list-([0-9]+)-([0-9]+).html /index.php?m=content&c=index&a=lists&catid=$1&page=$2 last; rewrite ^/tag-([^.])-([0-9]+)-([0-9]+).html /index.php?m=content&c=tag&catid=$2&tag=$1&page=$3 last; rewrite ^/comment-([0-9]+)-([0-9]+)-([0-9]+).html /index.php?m=comment&c=index&a=init&commentid=content_$1-$2-$3 last; rewrite ^/([^.]).html /index.php?m=member&c=index&a=$1 last; 3)DEDECMS偽靜態 rewrite "^/index.html$" /index.php last; rewrite "^/list-([0-9]+).html$" /plus/list.php?tid=$1 last; rewrite "^/list-([0-9]+)-([0-9]+)-([0-9]+).html$" /plus/list.php?tid=$1&totalresult=$2&PageNo=$3 last; rewrite "^/view-([0-9]+)-1.html$" /plus/view.php?arcID=$1 last; rewrite "^/view-([0-9]+)-([0-9]+).html$" /plus/view.php?aid=$1&pageno=$2 last; rewrite "^/tags.html$" /tags.php last; rewrite "^/tag-([0-9]+)-([0-9]+).html$" /tags.php?/$1/$2/ last; 4)Discuz7偽靜態 rewrite ^/archiver/((fid|tid)-[\w-]+.html)$ /archiver/index.php?$1 last; rewrite ^/forum-([0-9]+)-([0-9]+).html$ /forumdisplay.php?fid=$1&page=$2 last; rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ /viewthread.php?tid=$1&extra=page\%3D$3&page=$2 last; rewrite ^/space-(username|uid)-(.+).html$ /space.php?$1=$2 last; rewrite ^/tag-(.+).html$ /tag.php?name=$1 last; 5)DiscuzX偽靜態 rewrite ^([^.])/topic-(.+).html$ $1/portal.php?mod=topic&topic=$2 last; rewrite ^([^.])/article-([0-9]+)-([0-9]+).html$ $1/portal.php?mod=view&aid=$2&page=$3 last; rewrite ^([^.])/forum-(\w+)-([0-9]+).html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last; rewrite ^([^.])/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last; rewrite ^([^.])/group-([0-9]+)-([0-9]+).html$ $1/forum.php?mod=group&fid=$2&page=$3 last; rewrite ^([^.])/space-(username|uid)-(.+).html$ $1/home.php?mod=space&$2=$3 last; rewrite ^([^.]*)/([a-z]+)-(.+).html$ $1/$2.php?rewrite=$3 last; if (!-e $request_filename) { return 404; } 6)ECSHOP偽靜態 if (!-e $request_filename) { rewrite "^/index.html" /index.php last; rewrite "^/category$" /index.php last; rewrite "^/feed-c([0-9]+).xml$" /feed.php?cat=$1 last; rewrite "^/feed-b([0-9]+).xml$" /feed.php?brand=$1 last; rewrite "^/feed.xml$" /feed.php last; rewrite "^/category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-])-([0-9]+)-(.+)-([a-zA-Z]+)(.).html$" /category.php?id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5&page=$6&sort=$7&order=$8 last; rewrite "^/category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-])(.).html$" /category.php?id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5 last; rewrite "^/category-([0-9]+)-b([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.).html$" /category.php?id=$1&brand=$2&page=$3&sort=$4&order=$5 last; rewrite "^/category-([0-9]+)-b([0-9]+)-([0-9]+)(.).html$" /category.php?id=$1&brand=$2&page=$3 last; rewrite "^/category-([0-9]+)-b([0-9]+)(.).html$" /category.php?id=$1&brand=$2 last; rewrite "^/category-([0-9]+)(.).html$" /category.php?id=$1 last; rewrite "^/goods-([0-9]+)(.).html" /goods.php?id=$1 last; rewrite "^/article_cat-([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.).html$" /article_cat.php?id=$1&page=$2&sort=$3&order=$4 last; rewrite "^/article_cat-([0-9]+)-([0-9]+)(.).html$" /article_cat.php?id=$1&page=$2 last; rewrite "^/article_cat-([0-9]+)(.).html$" /article_cat.php?id=$1 last; rewrite "^/article-([0-9]+)(.).html$" /article.php?id=$1 last; rewrite "^/brand-([0-9]+)-c([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+).html" /brand.php?id=$1&cat=$2&page=$3&sort=$4&order=$5 last; rewrite "^/brand-([0-9]+)-c([0-9]+)-([0-9]+)(.).html" /brand.php?id=$1&cat=$2&page=$3 last; rewrite "^/brand-([0-9]+)-c([0-9]+)(.).html" /brand.php?id=$1&cat=$2 last; rewrite "^/brand-([0-9]+)(.).html" /brand.php?id=$1 last; rewrite "^/tag-(.).html" /search.php?keywords=$1 last; rewrite "^/snatch-([0-9]+).html$" /snatch.php?id=$1 last; rewrite "^/group_buy-([0-9]+).html$" /group_buy.php?act=view&id=$1 last; rewrite "^/auction-([0-9]+).html$" /auction.php?act=view&id=$1 last; rewrite "^/exchange-id([0-9]+)(.).html$" /exchange.php?id=$1&act=view last; rewrite "^/exchange-([0-9]+)-min([0-9]+)-max([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.).html$" /exchange.php?cat_id=$1&integral_min=$2&integral_max=$3&page=$4&sort=$5&order=$6 last; rewrite ^/exchange-([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.).html$" /exchange.php?cat_id=$1&page=$2&sort=$3&order=$4 last; rewrite "^/exchange-([0-9]+)-([0-9]+)(.).html$" /exchange.php?cat_id=$1&page=$2 last; rewrite "^/exchange-([0-9]+)(.).html$" /exchange.php?cat_id=$1 last; } 7)PHPWind偽靜態 rewrite ^(.)-htm-(.)$ $1.php?$2 last; rewrite ^(.*)/simple/([a-z0-9_]+.html)$ $1/simple/index.php?$2 last; 8)SaBlog2.0偽靜態 只帶月份的歸檔 rewrite "^/date/([0-9]{6})/?([0-9]+)?/?$" /index.php?action=article&setdate=$1&page=$2 last; 無分類翻頁 rewrite ^/page/([0-9]+)?/?$ /index.php?action=article&page=$1 last; 分類 rewrite ^/category/([0-9]+)/?([0-9]+)?/?$ /index.php?action=article&cid=$1&page=$2 last; rewrite ^/category/([^/]+)/?([0-9]+)?/?$ /index.php?action=article&curl=$1&page=$2 last; 歸檔、高級搜索 rewrite ^/(archives|search|article|links)/?$ /index.php?action=$1 last; 全部評論、標簽列表、引用列表 帶分頁 rewrite ^/(comments|tagslist|trackbacks|article)/?([0-9]+)?/?$ /index.php?action=$1&page=$2 last; tags rewrite ^/tag/([^/]+)/?([0-9]+)?/?$ /index.php?action=article&item=$1&page=$2 last; 文章 rewrite ^/archives/([0-9]+)/?([0-9]+)?/?$ /index.php?action=show&id=$1&page=$2 last; RSS rewrite ^/rss/([0-9]+)?/?$ /rss.php?cid=$1 last; rewrite ^/rss/([^/]+)/?$ /rss.php?url=$1 last; 用戶 rewrite ^/uid/([0-9]+)/?([0-9]+)?/?$ /index.php?action=article&uid=$1&page=$2 last; rewrite ^/user/([^/]+)/?([0-9]+)?/?$ /index.php?action=article&user=$1&page=$2 last; 地圖文件 rewrite sitemap.xml sitemap.php last; 自定義鏈接 rewrite ^(.*)/([0-9a-zA-Z-_]+)/?([0-9]+)?/?$ $1/index.php?action=show&alias=$2&page=$3 last; 9)SHOPEX偽靜態 if (!-e $request_filename) { rewrite ^/(.+.(html|xml|json|htm|php|jsp|asp|shtml))$ /index.php?$1 last; } 10)Typecho偽靜態 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; }
Nginx中的rewrite只能放在server{},location{},if{}中,並且只能對域名后邊的除去傳遞的參數外的字符串起作用。
6. rewrite偽靜態重寫的執行順序 [如下,前面的執行優先級依次大於后面的]
location =
location 完整路徑
location ^~ 路徑
location ~,或者location~* 正則順序
location 部分起始路徑
location /
五、Apache偽靜態配置和常用Rewrite偽靜態規則
1. Apache虛擬機配置及偽靜態規則
1)編輯Apache的conf目錄下的httpd.conf文件。 去除"# LoadModule rewrite_module modules/mod_rewrite.so"的注釋,開啟mod_rewrite.so模塊支持。 去除"# Include conf/extra/httpd-vhosts.conf"的注釋,引入虛擬機配置文件。 2) 編輯httpd-vhost.conf <VirtualHost *:80> #發生錯誤時將發送郵件 #ServerAdmin test@kevin.com #文檔根目錄 DocumentRoot "/data/www/httpd" #域名 ServerName www.kevin.com #錯誤日志 ErrorLog "logs/error.log" #訪問日志 CustomLog "logs/access.log" #配置rewrite相關選項 <Directory "/data/www/httpd"> #允許所有指令,這將允許.htaccess AllowOverride All #2.2的訪問控制配置,先檢查允許的條件,沒有允許的全部禁止,中間只能有一個逗號不能有空格 #Order Allow,Deny #Allow from All #2.4的訪問控制配置,效果等同以上 Require all granted </Directory> 3) 修改.htaccess #以下表示:如果存在目錄或文件則直接訪問,否則執行RewriteRule RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f #隱藏index.php RewriteRule ^(.*)$ index.php/$1 [L] 4) 重啟apache服務
2. Apache偽靜態配置示例
偽靜態就是將原來動態化的頁面址轉換成為靜態化的地址。 例如: 原訪問地址:http://www.test.com/list.php?page=123&id=456 偽靜態地址:http://www.test.com/list-123-456.html 操作方法: 1)首先確認Apache已經正確加載了mod_rewrite模塊 檢查httpd.conf中是否有LoadModule Rewrite_module modules/mod_Rewrite.so這段代碼,如沒有請加上。 2)策略配置。現有一個網站,根目錄為/var/www/html,動態頁面地址為/list.php?page=123&id=456,現在我們想要的效果是/list-123-456.html 2.1)使用httpd.conf來配置rewrite策略: 要使用httpd.conf文件來設置偽靜態策略,可以直接在httpd.conf中寫入如下代碼,如果網站是配置在VirtualHost中, 則將這段代碼加到對應的<VirtualHost hostname><VirtualHost>標簽內: <IfModule mod_rewrite.c> #輸入: list-123-456.html #輸出: list.php?page=123&id=456 RewriteEngine on RewriteRule ^(.*)list-([0-9]+)-([0-9]+)\.html$ $1list.php?page=$1&id=$2 </IfModule> 添加完成后重啟httpd服務后即可生效 2.2)使用.htaccess來配置rewrite策略 檢查httpd.conf中的<Directory />標簽配置,確認AllowOverride配置為All,這樣才能啟用.htaccess文件: <Directory /> Options FollowSymLinks AllowOverride All </Directory> 檢查httpd.conf中的AccessFileName參數,確認為.htaccess AccessFileName .htaccess 在網站根目錄下建立.htaccess文件,寫入如下內容: RewriteEngine on RewriteRule ^(.*)list-([0-9]+)-([0-9]+)\.html$ $1list.php?page=$2&id=$3 保存后重啟httpd服務即可生效 常見問題: 1)為何都按上面設置了卻還是無法靜態化? 答:很有可能是因為別的目錄設置項覆蓋了<Directory />標簽內的選項,導致.htaccess文件沒起作用。 這個問題一般出現在網站根目錄的Directory標簽中,在這個例子中,可以檢查<Directroy"/var/www/html">標簽內的AllowOverride參數是否設置為All。 2).htaccess文件放在網站根目錄,那子目錄也可以實現偽靜態嗎? 答:.htaccess默認對所在目錄下所有子目錄生效,但是如果子目錄中也放置了.htaccess文件,則該子目錄下的訪問規則以子目錄中的.htaccess文件為准。
3. Apache開啟偽靜態示例(修改"AllowOverride ALL",打開支持.htaccess偽靜態文件的功能)
偽靜態只是改變了URL的顯示形式,實際上還是網站頁面還是動態頁面。偽靜態的頁面后綴可以是html 、 htm 或者是目錄格式等。那么為什么要用偽靜態呢? 有兩點原因:1是seo優化,偽靜態有利於搜索引擎的收錄,能夠增加網站優化效果;2是url看起來簡單,網站URL給人專業性。 1)加載Rewrite模塊: 在conf目錄下httpd.conf中找到 LoadModule rewrite_module modules/mod_rewrite.so 2)允許在任何目錄中使用“.htaccess”文件,將“AllowOverride”改成“All”(默認為“None”): # AllowOverride controls what directives may be placed in .htaccess files. # It can be “All”, “None”, or any combination of the keywords: # Options FileInfo AuthConfig Limit # AllowOverride All # 把 AllowOverride None 改為 AllowOverride All,重啟一下apache服務器使配置生效,這樣就支持.htaccess文件了。 3)Apache Rewrite模塊的簡單應用 Rewrite的所有判斷規則均基於Perl風格的正則表達式,通過以下基礎示例能寫出符合自己跳轉需求的代碼。 3.1)請求跳轉 目的是如果請求為.jsp文件,則跳轉至其它域名訪問。 例如: 訪問www.clin003.com/a.php跳轉至b.clin003.com/b.php網頁,訪問www.clin003.com/news/index.php跳轉至b.clin003.com/news/index.php網頁 注意: 不是使用HTML技術中的meta或者javascript方式,因為www.clin003.com/a.php這個文件並不存在,用的是Apache2.2服務器中的Rewrite模塊。 修改 .htaccess或apche的配置文件httpd.conf文件,添加以下內容 RewriteEngine on #開啟Rewrite模塊 RewriteRule (.*)\.php$ http://b.clin003.com/$1\.jsp [R=301,L,NC] #截獲所有.jsp請求,跳轉到http://b.clin003.com/加上原來的請求再加上.php。R=301為301跳轉,L為rewrite規則到此終止,NC為不區分大小寫 3.2)域名跳轉 如果請求為old.clin003.com下的所有URL,跳轉至b.clin003.com RewriteEngine on #開啟Rewrite模塊 RewriteCond %{REMOTE_HOST} ^old.studenthome.cn$ [NC] #針對host為old.clin003.com的主機做處理,^為開始字符,$為結尾字符 RewriteRule (.*) http://b.clin003.com/$1 [R=301,L,NC] 3.3)防盜鏈 如果本網站的圖片不想讓其它網站調用,可以在 .htaccess或者apche的配置文件httpd.conf文件中添加以下內容 RewriteEngine on #開啟Rewrite模塊 RewriteCond %{HTTP_REFERER} !^$ #如果不是直接輸入圖片地址 RewriteCond %{HTTP_REFERER} !img.clin003.com$ [NC] #且如果不是img.clin003.com所有子域名調用的 RewriteCond %{HTTP_REFERER} !img.clin003.com/(.*)$ [NC] RewriteCond %{HTTP_REFERER} !zhuaxia.com [NC] RewriteCond %{HTTP_REFERER} !google.com [NC] RewriteCond %{HTTP_REFERER} !google.cn [NC] RewriteCond %{HTTP_REFERER} !baidu.com [NC] RewriteCond %{HTTP_REFERER} !feedsky.com [NC] RewriteRule (.*)\.(jpg|jpeg|jpe|gif|bmp|png|wma|mp3|wav|avi|mp4|flv|swf)$ http://clin003.com/err.jpg [R=301,L,NC] #截獲所有.jpg或.jpeg……請求,跳轉到http://clin003.com/err.jpg提示錯誤的圖片,注:該圖片不能在原域名下,也不能在該.htaccess文件有效控制的文件夾中 對配置做幾點補充說明: L 表明當前規則是最后一條規則,停止分析以后重寫 NC 不區分大小寫 QSA 追加請求的字符串 ^ 表示語句開始 $ 表示語句的結束 3.4)不需要定義.htaccess文件 在Apache2\conf\httpd.conf 最后一行添加 RewriteEngine On RewriteRule ^(.*)-htm-(.*)$ $1.php?$2
4. Apache各種跳轉(包括偽靜態)的配置
1)404跳轉: #vim /etc/httpd/conf/httpd.conf 在虛擬主機配置里添加一行:ErrorDocument 404 /404.html 2)301跳轉: 將不帶www的跳轉到帶www的:在根目錄下新建.htaccess文件,寫入: Options +FollowSymLinks RewriteEngine on RewriteCond %{HTTP_HOST} ^manyi.cc [NC] RewriteRule ^(.*)$ http://www.manyi.cc/$1 [L,R=301] 重定向到新域名: Options +FollowSymLinks RewriteEngine on RewriteRule ^(.*)$ http://www.manyi.cc/$1 [L,R=301] 3)在httpd.conf配置文件中配置: <VirtualHost *:80> ServerName manyi.cc RedirectMatch permanent ^/(.*) http://www.manyi.cc/$1 </VirtualHost> 使用正則進行301偽靜態配置:(將news.php?id=123這樣的地址轉向到news-123.html) Options +FollowSymLinks RewriteEngine on RewriteRule ^news-(.+)\.html$ news.php?id=$1
5. FastCGI加載PHP偽靜態設置的注意事項
默認的"RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]"規則在apache fastcgi模式下會導致"No input file specified". 修改成 RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1] 這樣就好了,地址正常重寫。 #php api模式,服務器能識別PATH_INFO RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] #php fastcgi模式 服務器不識別PATH_INFO RewriteRule ^(.*)$ index.php [E=PATH_INFO:$1,QSA,PT,L]