IIS 7.5伪静态文件web.config编写规则及301重定向方法

很久没有注意过伪静态这个事情了,因为平时都是使用的httpd.ini或者.htaccess来实现网站的伪静态 的。在我们公明本地有一位相当有资金实力的客户在我们为其开发完网站后,自购了一台服务器,商家配的系统是windows server 2008,IIS是7.5版本,网站上传后发现除了首页能访问外,其他页面打不开,也就是伪静态没有起作用。

IIS 7.5伪静态文件web.config编写规则及301重定向方法

IIS 7和IIS 7.5及以后的版本估计都会使用web.config来实现伪静态规则,于是我们以前的伪静态文件必须更改。网上找了一圈,还没有发现比较全面的web.config伪静态规则,于是我们这里整理一份,供初次使用的朋友参考。

实现普通页面、带一个数字参数页面和带两个参数页面的伪静态!

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>

                <rule name="Index" stopProcessing="true">
                    <match url="^index.html" />
                    <action type="Rewrite" url="index.php" />
                </rule>

                <rule name="Rule1" stopProcessing="true">
                    <match url="^news_([0-9]+).html" />
                    <action type="Rewrite" url="news.php?nid={R:1}" />
                </rule>
    
                <rule name="Rule2" stopProcessing="true">
                    <match url="news_list_([0-9]+)_([0-9]+).html" />
                    <action type="Rewrite" url="news_list.php?nid={R:1}&amp;page={R:2}" />
                </rule>

            </rules>
        </rewrite>
    </system.webServer>
</configuration>

IIS 7.5通过web.config实现301重定向的方法,将不带www的域名转向到带www的域名上!

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>

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

            </rules>
        </rewrite>
    </system.webServer>
</configuration>

由于我们的网站使用了转义字符,因此在实际使用的时候,大家不可以直接复制以上代码。请复制粘贴到Dreamweaver等编辑器后,使用替换功能把双引号全部替换为英文状态下的双引号,然后再修改rule标签内的内容就可以了,跳转的地方请更改为自己的网址即可。

需要注意的地方是以前httpd.ini和.htaccess支持网址中两个参数用&符号链接,在web.config中是不支持的,需要将这个符号更改为&才能正常使用。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://sulao.cn/post/179.html

我要评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。