当前位置:首页 > 建站知识 > 正文 】【打印

伪静态规则详解完整版

http://www.fived.com.cn  2021年6月27日

一.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="^fived.com.cn$" />
</conditions>
<action type="Redirect" url="http://www.fived.com.cn/{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="weixian.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 On
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^topic-(.+)\.html$ portal.php?mod=topic&topic=$1&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^article-([0-9]+)-([0-9]+)\.html$ portal.php?mod=view&aid=$1&page=$2&%1
RewriteCond %{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.abcd.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] 逻辑并集



作者:五天网络 来源:重庆网站建设
上一条: IIS禁止上传目录upload运行代码脚 下一条:网站变成灰色代码


(阅读次数:95次) 【 】【关闭】【打印返回顶部↑