nginx和apache下的url rewrite

将服务器上面的数据同步到本地以后,发现打开首页显示不正常,本地服务器是apache,通过打开url rewrite以后本地首页正常显示。php

缘由是phpwind自己支持了url rewrite的功能,可是本地的apache服务器没有开启这项功能,致使了首页的排版紊乱。css

远程服务器用的的nginx和本地的apache的url rewrite配置不能通用,借此机会学习下,url rewrite的功能。html

url rewrite是服务器的一个模块,功能包括,配置一些访问的网址的重写,其中的语句规则是基于正则表达式,建议先看个人另外一篇博客正则表式http://www.cnblogs.com/yiluxiuxing/p/4307488.htmlnginx

其中涉及到的变量都是基于服务器上(apache或者nginx)通用的变量,具体一些变量详细解释以及nginx下rewrite的一些配置实例请参考http://www.cnblogs.com/yiluxiuxing/p/4309365.html正则表达式

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

其余还有不少有用的功能,好比,防止别的网站引用你网站的图片,若是别人使用的是你网站的图片的话,那么占用的是你的网站的流量,可是却不能给你带来访问量浏览器

还好比能够本身写一个友好的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 }   

与之一部分对应的.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] 

RewriteEngine on RewriteCond %{ENV:REDIRECT_STATUS} 200 RewriteRule ^.*$ - [L]

#若是访问的网址文件不存在,则若是网址中出现/tool/则将网址重写为$1tool/index.php 网址中若出现/kisswall/同理 RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)tool/(.*)$ $1tool/index.php [L] RewriteRule ^(.*)kisswall/(.*)$ $1kisswall/index.php [L]
RewriteEngine On DirectoryIndex index.php index.html index.htm
#将以-分割的网址转换为$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