1、什麼是僞靜態
僞靜態便是網站自己是動態網頁如.php、.asp、.aspx等格式動態網頁有時這類動態網頁還跟"?"加參數來讀取數據庫內不一樣資料,僞靜態就是作url重寫操做(即rewrite)。很典型的案例便是discuz論壇系統,後臺就有一個設置僞靜態功能,開啓僞靜態後,動態網頁即被轉換重寫成靜態網頁類型頁面,經過瀏覽器訪問地址和真的靜態頁面沒區別。可是記住:作僞靜態的前提就是服務器要支持僞靜態重寫URL Rewrite功能。javascript
考慮搜索引擎優化(即SEO),將動態網頁經過服務器處理成靜態頁面,如www.kevin.com/jk/fd.php?=12這樣的動態網頁處理成www.kevin.com/jk-fd-12.html這樣格式靜態頁面,常見的論壇帖子頁面,都是通過僞靜態處理成靜態頁面格式html頁面。因爲網站所用的程序語言不易被發現,通過重寫來僞靜態來將動態網頁的程序後綴變爲html的靜態頁面格式。僞靜態是一種能夠把文件後綴改爲任何可能的一種方法,好比若是想把php文件僞靜態成html文件,這種配置至關簡單的,後面會提到相應配置。php
2、真靜態、僞靜態優勢和缺點
真靜態(html)優勢;1)減小服務器對數據響應的負荷;2)加載不用調動數據庫,響應速度快。
真靜態缺點:1)維護不方便,每次都要手動生成;2)空間佔用比較大,容易形成磁盤壓力;3)生成的文件多,服務器對html文件的響應負擔也較重。css
僞靜態(url重寫)優勢:1)能夠方便的實現對化化引擎的優化,而且比生成靜態更加方便;2)佔空間比較小;3)首頁天天都自動變化,不用維護。網站首頁通常都有熱點排行之類的,你能夠設爲,24小時排行,一週排行,再加上最新文章,最新點評等。這樣首頁每天是有變化的;4)便於廣告的輪顯。好比:你能夠把 art1234.aspx,這個虛成n個頁,如art_1234.aspx,news_1234.aspx,top_1234.aspx,在不一樣的頁面放 不一樣的廣告。總之是動態的,就能夠隨意動。
僞靜態缺點:1)若是流量稍大一些使用僞靜態就出現CPU使用超負荷,由於僞靜態是用正則判斷而不是真實地址,分辨到底顯示哪一個頁面的責任也由直接指定轉由CPU來判斷了,因此CPU佔有量的上升,確實是僞靜態最大的弊病;2)僞靜態效率不如生成html的,由於它不是真正意義上的靜態頁,因此每次請求都是要去讀取數據庫的信息(這個能夠用緩存技術來補償一下)。html
3、真靜態、僞靜態原理與實現方案
一、僞靜態
僞靜態是相對於真靜態而言的,就是把一些asp,php等結尾url經過apche或nginx的重寫規則,變成以html一類的靜態頁面形式。僞靜態不是真正的靜態,它和動態地址同樣要讀取數據庫。僞靜態最主要的做用就是利於seo,百度spider(百度蜘蛛)喜歡抓取靜態頁面,可容易使百度spider陷入死循環;併發量高的時候會加大服務器的壓力,因此用的時候要注意。java
僞靜態就是利用apche,nginx重寫規則,對url地址重寫實現的!僞靜態實現原理:
1) Apache僞靜態前提是要打開apache的重寫模塊 (即打開"LoadModule rewrite_module modules/mod_rewrite.so"這一行);
2) Nginx默認就支持僞靜態;linux
僞靜態有兩種配置方式
1) 在配置虛擬主機的時候設置;
2) 在web根目錄下建立一個.htaccess文件,在這個文件裏面配置;nginx
二、真靜態
在網站設計中,純粹HTML(標準通用標記語言下的一個應用)格式的網頁一般被稱爲"靜態網頁",靜態網頁是標準的HTML文件,它的文件擴展名是.htm、.html,能夠包含文本、圖像、聲音、FLASH動畫、客戶端腳本和ActiveX控件及JAVA小程序等。靜態網頁是網站建設的基礎,早期的網站通常都是由靜態網頁製做的。靜態網頁是相對於動態網頁而言,是指沒有後臺數據庫、不含程序和不可交互的網頁。靜態網頁相對更新起來比較麻煩,適用於通常更新較少的展現型網站。容易誤解的是靜態頁面都是htm這類頁面,實際上靜態也不是徹底靜態,它也能夠出現各類動態的效果,如GIF格式的動畫、FLASH、滾動字幕等。web
大型web項目優化中常常會考慮到使用真靜態,這樣在訪問量大的時候,能夠減小cpu的壓力,可是會生成大量的文件佔用網站的磁盤空間,能夠寫個php的腳本或用linux的計劃任務進行刪除。在用真靜態的時候有的時候須要用到局部的動態化。正則表達式
真靜態實現方法
1)利用PHP模板生成靜態頁面;
2)使用PHP文件讀寫功能生成靜態頁面;
3)使用PHP輸出控制函數緩存機制生成靜態頁面;
4)使用nosql從內存中讀取內容(其實這個已經不算靜態化了而是緩存);
memcached是鍵值一一對應,key默認最大不能超過128個字節,value默認大小是1M,所以1M大小知足大多數網頁大小的存儲。sql
真靜態和僞靜態的區別
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更有優點;
先來看個簡單的小例子
跳轉需求: 訪問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位數。
4、Nginx僞靜態配置和經常使用Rewrite僞靜態規則演示集錦
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; } }
僞靜態重寫
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 場景二: 把下面帶參數的第一、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位數。
nginx配置僞靜態的Rewrite重寫的正則使用說明
正則表達式匹配 : ~ 爲區分大小寫的匹配 ~* 不區分大小寫的匹配(匹配firefox的正則同時匹配FireFox) !~ 區分大小寫的不匹配 !~* 不區分大小寫的不匹配 . 匹配除換行符之外的任意字符 \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 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
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僞靜態配置案例
公司域名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配置僞靜態(去除框架的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 經常使用僞靜態配置
訪問 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; } }
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{}中,而且只能對域名後邊的除去傳遞的參數外的字符串起做用。
rewrite僞靜態重寫的執行順序:
(location =) > (location 完整路徑) > (location ^~ 路徑) > (location ~,~* 正則順序) > (location 部分起始路徑) > (/)
5、Apache僞靜態配置和經常使用Rewrite僞靜態規則演示集錦
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服務
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文件爲準。
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
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
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]