XAPMM Apache Rewrite url重定向功能的簡單配置

<VirtualHost *:80>
DocumentRoot G:\xampp\htdocs\web\centvjtqt\zhuanti
ServerName zhuanti.test.com
RewriteEngine on
RewriteRule ^/([a-zA-Z0-9]+)/$ /index.php?m=$1 [QSA,L]
RewriteRule ^/([a-zA-Z0-9]+)/([a-z0-9_]+)/$ /index.php?m=$1&a=$2 [QSA,L]
</VirtualHost>

以上爲虛擬主機配置 httpd-vhosts.conf



同樣功能在nginx配置如下:

vi virtual.conf

server {

       listen       80;
       server_name  zhuanti.test.com;
       charset utf-8;
       error_page  404              /404.html;
       error_page   500 502 503 504  /500.html;
       location / {
           root   /home/centv.com/zhuanti/;
           index  index.php index.html index.htm;
           if (!-f $request_filename){
                rewrite ^/([a-zA-Z_0-9]+)/$ /index.php?m=$1 last;
                rewrite ^/([a-zA-Z_0-9]+)/([0-9a-z_]+)/$ /index.php?m=$1&a=$2 last;
           }
       }
       location ~ .php$ {
           root           /home/centv.com/zhuanti/;
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
           include        fastcgi_params;

       }

   }


upload_max_filesize

post_max_size

max_execution_time

client_max_body_size



同虛似主機有不同的重定向要求:

分別用www.yuming.cn 和 www.yuming.cn/centvht/trunk/ 的方式訪問

server
{
    listen       80;  
    server_name  www.yuming.cn;  
    charset utf-8;

    location / {
 		root   /home/path/centvqt/zhuanti/;
        	index  index.php index.html index.htm;
  		if (!-f $request_filename){
  			rewrite ^/([a-zA-Z_0-9]+)/$ /index.php?m=$1 last;
  			rewrite ^/([a-zA-Z_0-9]+)/([0-9a-z_]+)/$ /index.php?m=$1&a=$2 last;
 		}
      }      
        
    location /centvht/trunk/ {
                root   /home/path/centvqt/zhuanti/;
                index  index.php index.html index.htm;
	        if (-e $request_filename) {   #文件或目錄 
		     break;
                }
                if (!-e $request_filename) {   #文件或目錄不存在
                     rewrite ^/(.+)$ /centvht/trunk/index.php?url=$1 last;
                     break;
                }      
      }

    location ~ \.php$
    {      
	root   /home/sobey/path/zhuanti/;
        #include vhosts/auth.inc;
        #fastcgi_pass unix:/tmp/php-fcgi.sock;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index  index.php;  
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;  
        include        fastcgi_params; 
    }
    access_log  /var/log/nginx/xxbjjtv.centv.cn.log;
    client_max_body_size 50m;
}




以下轉自:http://www.jb51.net/article/24435.htm

1.Apache Rewrite的主要功能 
就是實現URL的跳轉和隱藏真實地址,基於Perl語言的正則表達式規範。平時幫助我們實現擬靜態,擬目錄,域名跳轉,防止盜鏈等 

2.Apache Rewrite的配置 
Apache下的Rewrite配置主要有兩種,一種是針對整個apache服務器的配置,此種配置的Rewrite規則是直接在httpd.conf下書寫。配置步驟如下: 
(1)去除httpd.conf文件中"#LoadModule rewrite_module modules/mod_rewrite.so"前面的"#"號; 
(2)然後再在httpd.conf中書寫如下規則: 
RewriteEngine on 
#當訪問任何以t_開頭,以.html結尾的文件時,將$1用與(.*)匹配的字符替換後,訪問相應的test.php頁面 
RewriteRule ^/t_(.*).html$ /test.php?id=$1 

另一種是針對apache服務器下的某一目錄的配置,此種配置的Rewrite規則需在此目錄下建立一個.htaccess文件來書寫。配置步驟如下: 
(1)去除httpd.conf文件中"#LoadModule rewrite_module modules/mod_rewrite.so"前面的"#"號; 
(2)修改httpd.conf文件中的"AllowOverride None"爲"AllowOverride all",同時最好將Options也置爲"all",否則可能會出問題。 
(3)在目錄中建立.htaccess文件,並用記事本打開,書寫如下規則: 
RewriteEngine on 
RewriteRule ^/t_(.*).html$ /test.php?id=$1 

3.Apache Rewrite規則的書寫 
RewriteEngine on 
RewriteRule ^/test([0-9]*).html$ /test.php?id=$1 
RewriteRule ^/new([0-9]*)/$ /new.php?id=$1 [R] 

RewriteEngine on 
#當我們訪問的地址不是以www.163.com開頭的,那麼執行下一條規則 
RewriteCond %{HTTP_HOST} !^www.163.com [NC] 
RewriteRule ^/(.*) http://www.163.com/ [L] 

4.Apache Rewrite規則修正符 
1) R 強制外部重定向 
2) F 禁用URL,返回403HTTP狀態碼。 
3) G 強制URL爲GONE,返回410HTTP狀態碼。 
4) P 強制使用代理轉發。 
5) L 表明當前規則是最後一條規則,停止分析以後規則的重寫。 
6) N 重新從第一條規則開始運行重寫過程。 
7) C 與下一條規則關聯 

如果規則匹配則正常處理,以下修正符無效 

8) T=MIME-type(force MIME type) 強制MIME類型 
9) NS 只用於不是內部子請求 
10) NC 不區分大小寫 
11) QSA 追加請求字符串 
12) NE 不在輸出轉義特殊字符 \%3d$1 等價於 =$1

發佈了151 篇原創文章 · 獲贊 14 · 訪問量 47萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章