一.web.config 适用iis7以上

实例1:普通重写
.htaccess转换web.config   注意: 1.添加<match url="^(.*/)*  2. 添加 url="{R:1} 3.去掉 list.asp\?teacher_id  转义符

<?xml version="1.0" encoding="UTF-8"?><configuration><system.webServer><rewrite><rules><rule name="rule1"><match url="^(.*)t/([0-9,a-z]*)" ignoreCase="false" /><action type="Rewrite" url="{R:1}/t_list.asp?teacher_id={R:2}" appendQueryString="false" /></rule><rule name="rule2"><match url="^(.*/)*([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)\.html\?*(.*)$" /><action type="Rewrite" url="{R:1}/index.php?moduleid={R:2}&amp;catid={R:3}&amp;itemid={R:4}&amp;page={R:5}" /></rule></rules></rewrite></system.webServer></configuration>

实例2:其他重写功能

(1)301重定向

<rule name="301Redirect" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll"><add input="{HTTP_HOST}" pattern="^xxx.com$" /></conditions>
<action type="Redirect" url="http://www.xxx.com/{R:0}" redirectType="Permanent" />
</rule>

(2)取消目录执行权限

<rule name="rule1"><match url="uploads/(.*).(php|asp|aspx)$" ignoreCase="false" /><conditions logicalGrouping="MatchAll"><add input="%" pattern="^$" ignoreCase="false" negate="true" /></conditions><action type="AbortRequest" />
</rule><rule name="Rule2"><match url="(.*).(asp)$" ignoreCase="false" /><action type="AbortRequest" />
</rule>

(3)屏蔽来源域名

<rule name="rule1" stopProcessing="true"><match url="^(.*)$" /><conditions><add input="{HTTP_REFERER}" pattern="ylhqvip.com" negate="true" /><add input="{HTTP_REFERER}" pattern="^$" negate="true" /></conditions><action type="AbortRequest" />
</rule>

(4)屏蔽ip地址

<rule name="band ip" stopProcessing="true"><match url="(.*)" /><conditions logicalGrouping="MatchAny"><add input="%{HTTP_X_FORWARDED_FOR}&amp;%{REMOTE_ADDR}&amp;%{HTTP_X_Real_IP}" pattern="(8.8.4.4|8.8.8.)" /></conditions><action type="AbortRequest" />
</rule>

(5)过滤静态文件

<rule name="Imported Rule 1" stopProcessing="true"><match url="^(.*)$" ignoreCase="false" /><conditions logicalGrouping="MatchAll"><add input="{URL}" pattern="^.*(.css|.js|.gif|.png|.jpg|.jpeg|.xml)" ignoreCase="false" /></conditions><action type="None" />
</rule>

(6)屏蔽蜘蛛

<rule name="Block spider" stopProcessing="true"><match url="(.*)" ignoreCase="false" negate="false" /><conditions><add input="{HTTP_USER_AGENT}" pattern="baiduspider|googlebot" /></conditions><action type="AbortRequest" />
</rule> 

语法总结
(1)<add>条件判断,就像我们程序中的if语句一样,表示如果符合某个或某几个条件则执行action后的语句

#判断访问域名:     <add input="{HTTP_HOST}" pattern="^www.xxx.com$" />
#判断user_agent:   <add input="{HTTP_USER_AGENT}" pattern="baiduspider|googlebot" />
#判断访问来源域名: <add input="{HTTP_REFERER}" pattern="xxx.com" negate="true" />
#判断url中:        <add input="{URL}" pattern="^.*(.css|.js|.gif|.png|.jpg|.jpeg|.xml)" ignoreCase="false" />
#判断url中?后参数: <add input="{QUERY_STRING}" pattern="blog" negate="true" />
#判断url路径地址:  <add input="{REQUEST_URI}" pattern="blog" negate="true" />
#判断ip(包括代理): <add input="%{HTTP_X_FORWARDED_FOR}&amp;%{REMOTE_ADDR}&amp;%{HTTP_X_Real_IP}" pattern="(8.8.4.4|8.8.8.)" />
#判断真实文件:     <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" pattern="" ignoreCase="false" />
#判断真实目录:     <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" pattern="" ignoreCase="false" />
#判断match中的变量:<add input="{R:1}" pattern="^(bbs|blog)" ignoreCase="false" negate="true" />
#其他              <add input="%" pattern="^$" ignoreCase="false" negate="true" />

(2)<action>处理方式

#禁止访问:         <action type="AbortRequest" />
#重定向到          <action type="Redirect" url="http://www.xxx.com" redirectType="Permanent" />
#重写到            <action type="Rewrite" url="{R:1}/t_list.asp?teacher_id={R:2}" appendQueryString="false" />
#不做操作          <action type="None" />
#向客户端返回状态  <action type="CustomResponse" statusCode="403" statusReason="Forbidden" statusDescription="Forbidden" />

(3)参数

忽略大小写:             ignoreCase="true"|ignoreCase="false"
非(不等于):             negate="true"
不带?后面的参数:        appendQueryString="false"
永久重定向:             redirectType="Permanent"
匹配条件为所有还是一条: logicalGrouping="MatchAll"|logicalGrouping="MatchAny"      # 用于conditions节点

二.htaccess 适用iis6(rewrite3.1)|linux实例1:普通重写

<IfModule mod_rewrite.c>RewriteEngine OnRewriteCond %{QUERY_STRING} ^(.*)$RewriteRule ^topic-(.+)\.html$ portal.php?mod=topic&topic=$1&%1RewriteCond %{QUERY_STRING} ^(.*)$RewriteRule ^article-([0-9]+)-([0-9]+)\.html$ portal.php?mod=view&aid=$1&page=$2&%1RewriteCond %{QUERY_STRING} ^(.*)$RewriteRule ^forum-(\w+)-([0-9]+)\.html$ forum.php?mod=forumdisplay&fid=$1&page=$2&%1
</IfModule>RewriteRule ^(.*)/topic-(.+)\.html(\?(.*))*$ $1/portal\.php\?mod=topic&topic=$2&$4
RewriteRule ^(.*)/article-([0-9]+)-([0-9]+)\.html(\?(.*))*$ $1/portal\.php\?mod=view&aid=$2&page=$3&$5
RewriteRule ^(.*)/forum-(\w+)-([0-9]+)\.html(\?(.*))*$ $1/forum\.php\?mod=forumdisplay&fid=$2&page=$3&$5]

动态地址跳转到静态地址

RewriteRule ^goods-([0-9]+)(.*)\.html$  goods\.php\?id=$1 [QSA,L]RewriteCond %{QUERY_STRING} ^id=([0-9]+)$ [NC]
RewriteRule ^goods.php$ /goods-%1.html? [L,R=301]RewriteCond %{QUERY_STRING} ^id=1$ [NC]
RewriteRule ^category.php$ http://www.yaolongnonwoven.com/? [L,R=301] 加?不带参数,不加带上参数

实例2:其他重写功能

(1)301重定向

RewriteCond %{HTTP_HOST} ^xxxx1.com$ [NC]
RewriteCond %{HTTP_HOST} ^xxxx2.com$ [NC]
RewriteRule ^(.*)$ http://www.xxxx1.com/$1 [R=301,L]

(2)取消目录执行权限

RewriteCond % !^$
RewriteRule uploads/(.*).(php)$ – [F]
RewriteRule data/(.*).(php)$ – [F] 

(3)屏蔽来源域名

RewriteCond %{HTTP_REFERER} www.baidu.com [NC]
RewriteRule ^(.*)$ - [F]

(4)屏蔽ip地址

RewriteCond %{http:X-Forwarded-For}&%{REMOTE_ADDR}&%{http:X-Real-IP} (8.8.4.4|8.8.8.) [NC]
RewriteRule (.*) - [F]

(5)过滤静态文件

RewriteCond %{REQUEST_URI} ^.*(.css|.js|.gif|.png|.jpg|.jpeg|.xml)
RewriteRule ^(.*)$   - [L] RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

(6)屏蔽蜘蛛

RewriteCond %{HTTP_USER_AGENT} (baiduspider|googlebot) [NC]
RewriteRule ^(.*)$ - [F]

语法总结

(1)RewriteCond条件判断,就像我们程序中的if语句一样,表示如果符合某个或某几个条件则执行RewriteCond下面紧邻的

RewriteRule语句
#判断访问域名:     RewriteCond %{HTTP_HOST} ^xxxx.com$ [NC]
#判断user_agent:   RewriteCond %{HTTP_USER_AGENT} Baiduspider [NC]
#判断访问来源域名: RewriteCond %{HTTP_REFERER} www.baidu.com [NC]
#判断METHOD:       RewriteCond %{REQUEST_METHOD} ^(TRACE|OPTIONS)
#判断url中?后参数: RewriteCond %{QUERY_STRING} ^id=([0-9]+)$ [NC]
#判断url路径地址:  RewriteCond %{REQUEST_URI} ^/bbs
#判断ip(包括代理): RewriteCond %{http:X-Forwarded-For}&%{REMOTE_ADDR}&%{http:X-Real-IP} (8.8.4.4|8.8.8.) [NC]
#判断真实文件:     RewriteCond %{REQUEST_FILENAME} !-f
#判断真实目录:     RewriteCond %{REQUEST_FILENAME} !-d
#判断header        RewriteCond %{HTTP:header}
#判断以上所有情况: RewriteCond $1 !^(bbs|blog)
#其他 RewriteCond % !^$ 

(2)处理方式

#禁止访问:         RewriteRule (.*) - [F]
#重定向到          RewriteRule ^(.*)$ http://www.xxx.com/404.html [R=301,L]
#重写到            RewriteRule ^goods-([0-9]+)(.*)\.html$  goods\.php\?id=$1 [QSA,L]
#不做操作          RewriteRule ^(.*)$   - [L] 

参数解释:

$N 规则后向引用
%N RewriteCond 后向引用
${mapname:key|default}
%{VARNAME} 服务器变量
‘!’ 取非
[C] 与下一个规则联锁
[CO=name:val:domain:lifetime:path] 设置cookie
[F] 强制禁止应答
[G] 强制继续应答
[H=content-handler] 明确的内容处理 (不适用)
[L] 上一个规则标记
[N] 再次应用规则
[NC] 大小写不敏感
[NE] 不转义输出
[NS]非内部子请求
[P]代理通过
[QSA] 追加查询字符串
[R =code] 重定向
[S=num] 跳到下面第n条规则
[T=MIME-type] 强制明确应答 MIME 类型
RewriteCond
[NC] 大小写不敏感
[OR] 逻辑并集

三.nginx 规则

实例1.普通重写

location / { if (!-e $request_filename) { rewrite  ^(.*)$  /index.php?s=$1  last; break;   }
}

实例2.其他重写

(1).301重定向

server_name test.com www.test.com;
if ($host ~* test.com) { rewrite ^/(.*)$ http://www.test.com/$1 permanent;
}

http跳转https
普通

rewrite ^(.*) https://www.abc.com$1 permanent; 

有cdn

if ( $http_from_https != 'on' ){rewrite ^(.*) https://www.abc.com$1 permanent;      }

(2). 取消目录执行权限

location ~* ^/(uploads|templets|data)/.*.(php|php5)$ {deny  all;}

(3).屏蔽来源域名

location / {valid_referers www.baidu.com www.360.cn;if ($invalid_referer){return 403;}}

防盗链

location ~* \.(gif|jpg|png|webp)$ {valid_referers none blocked domain.com *.domain.com server_names ~\.google\. ~\.baidu\.;if ($invalid_referer) {return 403;#rewrite ^/ http://www.domain.com/403.jpg;}root /opt/www/image;}

(4).屏蔽ip地址

allow 1.1.1.2;
allow all;
deny all;
deny 1.1.1.2location ^~ /xxx/xxx/xx/{allow 172.0.0.1;allow xxx.xxx.0.0/8;#表示允许xxx.xxx.0.1 ~ xxx.xxx.255.254  allow xxx.0.0.0/16;#表示允许xxx.0.0.1 ~ xxx.255.255.254allow xxx.xxx.xxx.x; deny all;}

前端还有cdn情况

 map $http_x_forwarded_for  $clientIp {""      $remote_addr;~^(?P<firstAddr>[0-9\.]+),?.*$  $firstAddr;
}
if ($clientIp ~* "127.0.0.1|127.0.0.2") {return 403;break;
}

(5).屏蔽蜘蛛

if ($http_user_agent ~ "FeedDemon|JikeSpider|MJ12bot|heritrix|EasouSpider|LinkpadBot|Ezooms" )
{return 403;
}

(6).禁止非GET|HEAD|POST方式的抓取

if ($request_method !~ ^(GET|HEAD|POST)$) {return 403;}

语法总结
(1)RewriteCond条件判断,就像我们程序中的if语句一样,表示如果符合某个或某几个条件则执行RewriteCond下面紧邻的RewriteRule语句

#判断访问域名:     if ($host ~* test.com)
#判断user_agent:   if ($http_user_agent ~* "baiduspider" )
#判断访问来源域名: valid_referers www.baidu.com;if ($invalid_referer){return 403;}
#判断METHOD:       if ($request_method !~ ^(GET|HEAD|POST)$)
#判断url中?后参数: if ($request_uri ~* ^/list.php\?([^_]+)(_[0-9]+)$)
#判断url路径地址:  if ($uri ~* ^/list.php\?([^_]+)(_[0-9]+)$)
#判断ip:           if ($remote_addr ~* "127.0.0.1|127.0.0.2")
#判断真实文件:     -e filename
#判断真实目录:     -d filename

(2).处理方式

#禁止访问:         return 403; deny all;
#重定向到          rewrite ^/(.*)$ http://www.test.com/$1 permanent;
#重写到            rewrite  ^(.*)$  /index.php?s=$1  last; 
last – 基本上都用这个Flag。
break – 中止Rewirte,不在继续匹配
redirect – 返回临时重定向的HTTP状态302
permanent – 返回永久重定向的HTTP状态301-f和!-f用来判断是否存在文件
-d和!-d用来判断是否存在目录
-e和!-e用来判断是否存在文件或目录
-x和!-x用来判断文件是否可执行

参数解释

location [=|~|~*|^~] /uri/ { … }
= 开头表示精确匹配
^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。
~ 开头表示区分大小写的正则匹配
~* 开头表示不区分大小写的正则匹配
!~和!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则
/ 通用匹配,任何请求都会匹配到。

全局变量

$args
$content_length
$content_type
$document_root
$document_uri
$host
$http_user_agent
$http_cookie
$limit_rate
$request_body_file
$request_method
$remote_addr
$remote_port
$remote_user
$request_filename
$request_uri
$query

常见rewrite规则相关推荐

  1. [转载]apache rewrite规则

    转载自: http://hi.baidu.com/lzpsky/item/62bce176c443435f0c0a0726 apache服务器使用时配置经常用到,Mark以下以备留用. Rewrite ...

  2. nginx rewrite规则语法

    在新主机的迁移过程中,最大的困难就是WP permalink rewrite的设置. 因为旧主机是用的Apache, 使用的是WP本身就可以更改的.htaccess,没有太大的难度.而这次在VPS上跑 ...

  3. nginx rewrite规则语法(关于break和last分析得很好)

    在新主机的迁移过程中,最大的困难就是WP permalink rewrite的设置. 因为旧主机是用的Apache, 使用的是WP本身就可以更改的.htaccess,没有太大的难度.而这次在VPS上跑 ...

  4. ginx rewrite规则语法

    1 Nginx rewrite基本语法 Nginx的rewrite语法其实很简单.用到的指令无非是这几个 set if return break rewrite 麻雀虽小,可御可萝五脏俱全.只是简单的 ...

  5. nginx配置文件rewrite规则

    nginx配置文件rewrite规则 文章目录 nginx配置文件rewrite规则 @[toc] if Rewite 规则介绍 flag标志位 配置rewrite规则 last二次转发 if 语法: ...

  6. [Jeson]Nginx入门到实践1-4章 ,关闭iptables,nginx重启,nginx缓存, 跨站访问,Rewrite规则,Https服务优化

    第4章 Nginx 与 Lua nginx 与 Lua Https服务优化 激活keepalive长连接 设置ssl session 缓存 动静分离 Rewrite规则 nginx 常见正则匹配符号表 ...

  7. Apache的Rewrite规则详细介绍

    rewrite是一种服务器的重写脉冲技术,它可以使得服务器可以支持 URL 重写,是一种最新流行的服务器技术.它还可以实现限制特定IP访问网站的功能. 工具/原料 Apache 方法/步骤 Rewri ...

  8. Apache Rewrite规则详解[转]

    1.Rewrite规则简介:Rewirte主要的功能就是实现URL的跳转,它的正则表达式是基于Perl语言.可基于服务器级的(httpd.conf)和目录级的(.htaccess)两种方式.如果要想用 ...

  9. nginx配置location总结及rewrite规则写法

    原文连接:http://seanlook.com/2015/05/17/nginx-location-rewrite/ 1. location正则写法 一个示例: 1 2 3 4 5 6 7 8 9 ...

最新文章

  1. Nutch插件开发及发布流程
  2. Java数据持久层框架 MyBatis之API学习八(Java API详解)
  3. libslog---高性能开源C/C++多线程安全日志库
  4. 百余名欧洲议会议员发函 呼吁英国留在欧盟
  5. HDFS应用场景、部署、原理与基本架构
  6. 1443B. Saving the City
  7. mysql 5.6.17 备份还原_mysql 备份和恢复
  8. dump mysql database,Mysql Database 逻辑备份: mysqldump
  9. C语言数组指针和指向数组的指针变量—数组名作函数参数.doc
  10. 我(阿朱)再说两句新零售
  11. 用计算机求和公式,全国计算机一级等考excel之sum求和函数
  12. PD虚拟机设置替代ALT+F4(关闭窗口)的快捷键的方法
  13. 华为路由器6to4隧道原理及配置
  14. 使用Docker提交代码参加天池比赛流程
  15. BIM究竟是什么?为什么设计院还不能普及BIM做设计?
  16. BSN与区块链云服务的区别是什么?
  17. 数据库仓工具箱及阿里大数据之路--阅读总结
  18. 机房环境监控系统的作用,机房监控的主要目的是什么
  19. 基于WSL搭建ESP8266开发环境
  20. Excel函数 - 时间函数

热门文章

  1. 毕业季.公司 | 百度:AI技术哪家强,度厂科大最在行?
  2. 七牛云 X FaceU 激萌:自拍软件玩起了短视频社交,AI 内容审核献助攻
  3. 李东生的功守道:收桑榆,失东隅
  4. confirm的使用方法
  5. 桌面上 计算机 回收站不见了怎么办,电脑回收站不见了,教您电脑回收站不见了怎么办...
  6. centos7 arm内核配置yum源
  7. 连接网络远程计算机没有反应,无线宽带路由器TP-LINK无法连接网络,显示远程计算机无反应。...
  8. 移植 Python 项目到容器 (Container) 中
  9. android和ios龙之谷,《龙之谷2手游》安卓和ios互通吗 安卓和苹果互通情况解析
  10. 28HTML5期末大作业:动漫网页设计作业网站设计——电影动漫言叶之庭(4页) HTML+CSS+JavaScript 学生DW网页设计作业成品 web课程设计网页规划与设计 计算机毕设计源码