iis网站用Web.config文件设置301和404跳转,

在网站部署上线的时候一般都需要设置域名不带www的301从定向到带www,和404错误跳转页面,那么在iis环境下改该如何设置呢。

1、Web.config 设置不带www域名301重定向到带www的域名

   在web.config文件中,代码<system.webServer> </system.webServer> 中间添加如下代码

列:zzsi.net 301到 www.xxxx.net 或者 http 从重定向到https,destination的值就是你要重定向指定的域名

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<httpRedirect enabled="true" destination="http://www.xxxx.net$S$Q" exactDestination="true" httpResponseStatus="Permanent" />
</system.WebServer>
</configuration>

对于域名后面的“$S$Q”,有些博客说加不加都行,在这里总结一下:

1:如果你实现的重定向都是指定到同一个页面,或者是,你的网站无论点击那个页面,url地址都是没有改变的,那么这种情况加不加都无所谓

2:如果你的url地址有其他参数,就域名后面还有其他参数的话,那一定要加上“$S$Q”

建议加上

2、iis开启多网址重定向功能

比如

www.xxxx.net/news/  301跳转到  www.xxxxi.net/new/

www.xxxx.net/chanpin/  301跳转到  www.xxxxx.net/product/

www.xxxxx.net/anli/3.html  301跳转到  www.xxxx.net/case/5.html

前言:iis需要先打开URL重写功能

 1、在IIS上安装ARR,下载地址(Application Request Routing : The Official Microsoft IIS Site)

本站下载地址:https://download.csdn.net/download/qq_39339179/74387020

2、完成后打开,Microsoft Web Platform Installer

3、搜索arr关键字,安装应用程序请求路由

 4、搜索url关键字,安装url重写工具

 5、重启服务器,才能在iis目录下看到新安装的ARR

 6、打开ARR,在右侧功能菜单中点击   Server Proxy Settings

 7、勾选 Enable proxy,然后点击应用

 8、打开你要设置URL重写的网站目录,此时就能看到一个URL重写的功能

 9、双击打开,添加规则

此时可以在当前站点增加Index.html页面,里面说明即将要做的事情。

然后规则名随便起,模式输入:^(./*)  代表匹配当前网站192.168.1.223:8700,跳转规则为输入  192.168.1.223:8700/ss  则默认都跳转至新页面

下面的URL重定向页面直接写你的目标地址即可 

PS:如果想在输入 192.168.1.223:8700的地址时就立刻跳转到百度,则需要设置HTTP重定向

开启iisURL重写功能后,也可以直接用web.config文件重定向地址

2、Web.config设置 一个网址301到另一个网址

列 www.zzsi.net/index1.html   301到  www.zzsi.net/index2.html 

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
    <rewrite>
        <rules>
            <rule name="Foo_To_Bar" stopProcessing="true">
                <match url="^index1.html" />
                <action type="Redirect" url="/index2.html" redirectType="Temporary" />
            </rule>
        </rules>	
    </rewrite>
</system.webServer>
</configuration>

3、多网址一对一301跳转

列:

www.xxxx.net/news/  301跳转到  www.xxxxi.net/new/

www.xxxx.net/chanpin/  301跳转到  www.xxxxi.net/product/

www.xxxxi.net/anli/3.html  301跳转到  www.xxxxi.net/case/5.html

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
    <rewrite>
        <rules>
	            <rule name="Foo_To_Bar" patternSyntax="ExactMatch" stopProcessing="true">
					<match url="news/" />
					<action type="Redirect" url="/new/" redirectType="Permanent" />
				</rule>
                <rule name="重定向规则1" patternSyntax="ExactMatch" stopProcessing="true">
                    <match url="chanpin/" />
                    <conditions logicalGrouping="MatchAny">
                    </conditions>
                    <action type="Redirect" url="/product/" appendQueryString="false" redirectType="Permanent" />
                </rule>
				<rule name="重定向规则2" patternSyntax="ExactMatch"  stopProcessing="true">
                    <match url="anli/3.html " />
                    <conditions logicalGrouping="MatchAny">
                    </conditions>
                    <action type="Redirect" url="/case/5.html" appendQueryString="true" />
                </rule>
        </rules> 
    </rewrite>
</system.webServer>
</configuration>

4、Web.config 设置404错误跳转,并设置404返回状态码为 404

 在web.config文件中,代码<system.webServer> </system.webServer> 中间添加如下代码

 <httpErrors errorMode="DetailedLocalOnly">
         <remove statusCode="404" />
         <error statusCode="404" path="/404.aspx" responseMode="ExecuteURL"/>
       </httpErrors> 

注意: 如果是asp网站,把404页面改为404.aspx格式在头部添加  如下代码即可 设置返回状态码为404

<% Response.Status = "404 Not Found"  %> 

如果是php网站的话把404页面改为404.php格式在头部添加  如下代码即可 设置返回状态码为404

<?php header(”HTTP/1.0 404 Not Found”);  ?>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

永远的WEB小白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值