nginx和apache下的url rewrite
来源:互联网 发布:销售管理论文数据 编辑:程序博客网 时间:2024/05/17 04:22
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.php13 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 #禁止htaccess46 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 OnRewriteCond %{HTTP_HOST} !^localhost [NC]RewriteCond %{HTTP_HOST} !^127.0.0.1 [NC]RewriteCond %{HTTP_HOST} !^$RewriteRule ^(.*) http://localhost/$1 [L]<br>RewriteEngine onRewriteCond %{ENV:REDIRECT_STATUS} 200RewriteRule ^.*$ - [L]<br><br>#如果访问的网址文件不存在,则如果网址中出现/tool/则将网址重写为$1tool/index.php 网址中若出现/kisswall/同理RewriteEngine OnRewriteCond %{REQUEST_FILENAME} !-fRewriteRule ^(.*)tool/(.*)$ $1tool/index.php [L]RewriteRule ^(.*)kisswall/(.*)$ $1kisswall/index.php [L]<br>RewriteEngine OnDirectoryIndex index.php index.html index.htm<br>#将以-分割的网址转换为$1.php?$2的格式RewriteRule ^(.*)-htm-(.*)$ $1.php?$2RewriteCond %{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
0 0
- nginx和apache下的url rewrite
- nginx和apache下的url rewrite
- Apache和Nginx设置伪静态(URL Rewrite)的方法
- Nginx下使Thinkphp URL模式支持PATHINFO和REWRITE
- Nginx下使Thinkphp URL模式支持PATHINFO和REWRITE
- Apache下伪静态html(URL Rewrite)的设置方法
- nginx下的rewrite
- apache支持URL Rewrite的配置和应用
- Apache Rewrite实现URL的跳转和域名跳转
- Apache Rewrite实现URL的跳转和域名跳转
- Apache Rewrite实现URL的跳转和域名跳转
- Apache Rewrite实现URL的跳转和域名跳转
- Apache Rewrite实现URL的跳转和域名跳转
- Apache Rewrite实现URL的301跳转和域名跳转
- Apache Rewrite实现URL的跳转和域名跳转
- Apache Rewrite实现URL的跳转和域名跳转
- 浅谈apache和nginx的rewrite的区别
- nginx 和 apache rewrite 规则的小错误
- 关于Your dtc is too old, please upgrade to dtc 1.4 or newer的解决方法
- 轮回 第九章 离去
- MySQL 的索引
- github基本使用
- Zbrush4r7|3D建模雕刻软件
- nginx和apache下的url rewrite
- 顺序表存储结构
- leetcode 445. Add Two Numbers II
- MySQL主从复制技术(纯干货)
- 轮回 第十章 醒来,吃馒头?
- Tensorflow的Bazel编程(四)
- platform之按键驱动多种写法(笔记)
- Effective c++ 读书笔记 4
- 命令模式