nginx和apache下的url rewrite


url rewrite是服务器的一个模块,功能包括,配置一些访问的网址的重写,其中的语句规则是基于正则表达式.

其中涉及到的变量都是基于服务器上(apache或者nginx)通用的变量,具体一些变量详细解释以及nginx下rewrite的一些配置实例请参考ngnix的rewrite参数,用法。

比如为了使网址更加友好,可以将用户看到的网址www.simple.com/ming-tian-shi-ge-hao-tian-qi.html重定位到www.simple.com/ming/tian/shi/ge/hao/tian/qi.html,这样用户看到的就是一个网址而不是一个个的文件夹。

其他还有很多有用的功能,比如,防止别的网站引用你网站的图片,如果别人使用的是你网站的图片的话,那么占用的是你的网站的流量,但是却不能给你带来访问量

还比如可以自己写一个友好的404页面,如果发生404错误的时候就将页面定位到自己写的404页面。

还可以将css文件还有js文件设置保存在用户浏览器上面的时间,加快网页的加载速度。

下面是nginx上面的rewrite配置文件


 1 server {
 2         listen  80;
 3         server_name www.simple.com ;
 4         root /home/www/simple;
 5         index index.php index.html index.htm;
 6         charset utf-8;
 7         access_log logs/simple.access.log main;
 8         #如果请求主机字段不等于'www.simple.com'则重定向到http://www.simple.com/*
 9         if ( $host != 'www.simple.com' ) {
10                 rewrite ^/(.*)$ http://www.simple.com/$1 permanent;
11         }
12         #如果当前请求的文件路径不存在,将出现/tool/的网址重定向到/tool/index.php,将出现kisswall/的网址重定向到/kisswall/index.php
13         if ( !-e $request_filename ) { 
14                 rewrite ^(.*)tool/(.*)$ $1tool/index.php last;
15                 rewrite ^(.*)kisswall/(.*)$ $1kisswall/index.php last;
16         }
17         location / {
18                 directio 1;
19                 output_buffers 1 128k;
20                 index index.php index.html index.htm ;
21                 rewrite ^(.*?)-(.*)$ $1.php?$2;
22         }
23         #指定404错误页面
24         error_page 404 /404.html;
25                 location = /50x.html {
26         }
27         #设置js、css过期时间
28         location ~ \.(css|js)$ {
29                 expires 1w;
30         }
31         #防盗链
32         location ~ \.(jpg|jpeg|png|gif|swf|ico)$ {
33                 valid_referers none bloacked *.erqilu.com *.renren.com *.weibo.com;
34                 if ( $invalid_referer ) {
35                         return 404;
36                 }
37                 expires max;
38         }    
39         location ~ \.php$ {
40             fastcgi_pass   127.0.0.1:9000;
41             fastcgi_index  index.php;
42             fastcgi_param  SCRIPT_FILENAME  /$document_root$fastcgi_script_name;
43             include        fastcgi_params;        
44         }    
45         #禁止htaccess
46         location ~ /\.ht { 
47                 deny all;
48         }
49         #将出现/min/的网址定位到/min/index.php?*
50         location /min/{ 
51                 rewrite /min/([a-z]=.*)  /min/index.php?$1 last;
52                 #expires 1w;
53         }
54 }   
apache对应的.htaccess文件
如果访问的网址不是“localhost”或者“127.0.0.1”则跳转到http://localhost/
 
RewriteEngine On
RewriteCond %{HTTP_HOST} !^localhost [NC]
RewriteCond %{HTTP_HOST} !^127.0.0.1 [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*) http://localhost/$1 [L]
<br>
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^.*$ - [L]<br><br>#如果访问的网址文件不存在,则如果网址中出现/tool/则将网址重写为$1tool/index.php 网址中若出现/kisswall/同理
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)tool/(.*)$ $1tool/index.php [L]
RewriteRule ^(.*)kisswall/(.*)$ $1kisswall/index.php [L]
<br>
RewriteEngine On
DirectoryIndex index.php index.html index.htm<br>#将以-分割的网址转换为$1.php?$2的格式
RewriteRule ^(.*)-htm-(.*)$ $1.php?$2
RewriteCond %{HTTP_HOST} !^localhost [NC]
RewriteRule ^(.*)  http://localhost/$1 [L]
#定义404页面
ErrorDocument 404 /404.html
#防盗链
RewriteBase /
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://localhost/.*$ [NC]
RewriteRule .(jpg|jpeg|png|gif|swf|ico)$ - [R=302,L]

对照可参考apache和nginx配置的异同,

其中nginx的url rewrite配置文件存放的位置是:usr/local/nginx/conf


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章