[英]Blocking invalid URL and redirect to homepage in IIS
Currently I am using the web hosting service provided by another company. 目前,我正在使用另一家公司提供的网络托管服务。 It uses IIS 7/IIS 8 and allows customers to modify
web.config
according to their needs. 它使用IIS 7 / IIS 8,并允许客户根据需要修改
web.config
。
Suppose I have set up two valid URL: 假设我设置了两个有效的URL:
Now, when I try to access http://abc.example.com
, HTTP Error 403.14 - Forbidden
is returned; 现在,当我尝试访问
http://abc.example.com
,返回HTTP Error 403.14 - Forbidden
访问; and if I then try to access http://www.example.com/abc
, HTTP Error 404.0 - Not Found
is returned. 并且如果我随后尝试访问
http://www.example.com/abc
,则返回HTTP Error 404.0 - Not Found
。
How can I configure web.config
such that users will be redirected to http://www.example.com
when they are trying to access an invalid URL? 我如何配置
web.config
,以便在尝试访问无效URL时将用户重定向到http://www.example.com
?
I have tried to use the snippet provided by the company but without success: 我尝试使用公司提供的代码段,但未成功:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Canonical Host Name" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
In system.web in webconfig tag add this tag 在system.web中的webconfig标签中添加此标签
<customErrors mode="On" defaultRedirect="~/Home/Index">
</customErrors>
This will redirect you to the home page whenever an error occured You can be more specific with redirection by adding in customErrors tag 每当发生错误时,这会将您重定向到主页。您可以通过添加customErrors标记来更详细地重定向
<error statusCode="401" redirect="/error/otherpage"/>
This will indicate the page that you will be redirected to when 401 error occurs 这将指示发生401错误时将您重定向到的页面
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.