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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章