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

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

其中nginx的url rewrite配置文件存放的位置是:usr/local/nginx/conf
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值