IIS 301重定向伪静态web.config规则

 1596

IIS 301重定向伪静态web.config规则


IIS7以上版本的空间配置伪静态等内容与IIS6与很大区别的,IIS6是在httpd.conf文件中写配置规则,而IIS7则是在根目录下的web.config文件中写规则,而且写法也大有不同。

如果你的网站已有web.config文件,则把以下规则直接写在节点 中就可以,如果没有web.config文件,需要在根目录新建一个,内容按照下面的完全复制即可,注意:所有配置规则都要写在节点中。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
    <rewrite>
    <rules>
        <rule name="301Redirectwww" stopProcessing="true">
        <match url="(.*)"/>
        <conditions logicalGrouping="MatchAny">
        <add input="{HTTP_HOST}" pattern="^zztuku\.com$"/>
        </conditions>
        <action type="Redirect" url="https://www.zztuku.com/{R:0}" redirectType="Permanent"/>
      </rule>
     </rules>
     </rewrite>
    </system.webServer>
</configuration>


一、网站301重定向

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

要注意的是,所有规则的name名字不能一样,这里的“301Redirectwww”可以自己随意命名。这段代码可以实现将不带www的网址定向到带www的,同理,其他域名跳转把网址做相应改变。


二、去index.html、index.php等后缀

<rule name="Redirectindex" stopProcessing="true">
    <match url="^index.html"/>
    <conditions logicalGrouping="MatchAny"/>
    <action type="Redirect" url="https://www.zztuku.com/" redirectType="Permanent"/>
</rule>

根据自己的需求,将要去掉的后缀名替换。


三、伪静态规则

<rule name="Redirecttagsl" stopProcessing="true">
    <match url="^tags/(\w+)/([0-9]+).html"/>
    <action type="Rewrite" url="tags.php?/{R:1}/{R:2}/"/>
</rule>

1、伪静态规则根据不同的网址形式有不同的写法,只是提供一个参考,其中通配符与百度移动适配中提到的正则式相同,大家可以参考《百度优化之移动适配代码正则表达式适配》。

2、规则语句中,match语句中网址前不能加“/”:

<match url="^tags/(\w+)/([0-9]+).html"/>

网址tags前面的“/”没有。

3、action语句中不能使用转义符:

<action type="Rewrite" url="tags.php?/{R:1}/{R:2}/"/>

网址中特殊符号并不需要转义。

4、伪静态规则中, {R:1}、{R:2}中1、2等数字代表与目标网址中的参数对应,第一个参数后面对应要写1,以此类推。


本文网址:https://www.zztuku.com/detail-13355.html
站长图库 - IIS 301重定向伪静态web.config规则
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐