IIS7 伪静态 web.config 配置方法【详解】

IIS7 做伪静态比较的简单方便  

1.程序方面

只需要设置web.config 就可以了。


2.服务器需要安装:URL Rewrite
下载地址:http://www.iis.net/download/URLRewrite
Godaddy 的主机已经安装这个插件。
本地在测试的时候 请查看自己是否安装这个插件。

 

 
注意要点
1.参数用“()” 括起来 ,使用 {R:1}来获得参数
2.多个参数中间用 & 分割
3.name切记不能写一样 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?xml version="1.0"?>
 
<configuration>
<system.webServer>
        <rewrite>
            <rules>
                <!--301重定向把不带3W的域名 定向到带3W-->
                <rule name="Redirect" stopProcessing="true">
                    <match url=".*" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^haoxinwen.info$" />
                    </conditions>
                    <action type="Redirect" url="http://www.haoxinwen.info/{R:0}" redirectType="Permanent" />
                </rule>
                <!--首页-->
                <rule name="rD">
                    <match url="^$" />
                    <action type="Rewrite" url="Default.aspx" />
                </rule>
                <!--产品列表-->
                <rule name="rP">
                    <match url="^product/$" />
                    <action type="Rewrite" url="ProductList.aspx" />
                </rule>
                <!--产品列表第几页-->
                <rule name="rPL">
                    <match url="^product/list-([0-9]*).html$" />
                    <action type="Rewrite" url="ProductList.aspx?page={R:1}" />
                </rule>               
                <!--产品类别列表-->
                <rule name="rPT">
                    <match url="^product/([A-Za-z0-9-]*)/$" />
                    <action type="Rewrite" url="ProductList.aspx?typeUrl={R:1}" />
                </rule>
                <!--产品类别列表第几页-->
                <rule name="rPTL2">
                    <match url="^product/([A-Za-z0-9-]*)/list-([0-9]*).html$" />
                    <action type="Rewrite" url="ProductList.aspx?typeUrl={R:1}&page={R:2}" />
                </rule>
                <!--产品详细-->
                <rule name="rPd">
                    <match url="^product/([A-Za-z0-9-]*)/([A-Za-z0-9-]+).html$" />
                    <action type="Rewrite" url="ProductDetail.aspx?typeUrl={R:1}&url={R:2}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

  

 

举个栗子:

安装URL Rewrite

 

在iis新建了一个叫做hello的站点,端口:8088

image.png

 站点目录新建一个index.html文件

 

1
<h1>hello world</h1>

  

启动站点,浏览器输入:http://localhost:8088/

ok,站点已经成功运行

 

打开站点目录,新建一个web.config 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0"?>
<configuration>
<system.webServer>
        <rewrite>
            <rules>
                <!--我的规则-->
                <rule name="myrule">
                    <match url="^hello$" />
                    <action type="Rewrite" url="index.html" />
                </rule>
                <!--我的规则2-->
                <rule name="myrule2">
                    <match url="^jy/good$" />
                    <action type="Rewrite" url="jy/good.html" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

ps:安装 URL Rewrite 后才可以使用rewrite标签

 

 启动站点,打开浏览器,输入路由地址:http://localhost:8088/jy/good

然后会匹配到站点的 jy/good.html 文件,如下:

 

good.html

1
<h1>Good</h1>

  

 浏览器显示:


服务热线