阻止访问没有asp.net URL重定向的站点

时间:2013-02-28 14:46:04

标签: asp.net url-rewriting url-redirection

我有三个网站。一个主站点和两个子站点位于一个discountasp.Net服务器上的文件夹中 - www.sitea.com,www.siteb.com和www.sitec.com。

我使用web.config文件中的URL重定向将流量重定向到子站点(siteb和sitec),因此:

<rule name="siteb" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^www.siteb.com$" />
            <add input="{PATH_INFO}" pattern="^/siteb/" negate="true" />
          </conditions>
          <action type="Rewrite" url="\siteb\{R:0}" />
        </rule>
        <rule name="sitec" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^www.sitec.com$" />
            <add input="{PATH_INFO}" pattern="^/sitec/" negate="true" />
          </conditions>
          <action type="Rewrite" url="\sitec\{R:0}" />
        </rule>

我的问题是我不希望公众能够输入www.sitea.com/siteb/并以这种方式访问​​子网站。

我如何配置我的服务器来阻止它?

由于

2 个答案:

答案 0 :(得分:0)

页面加载时,您可以获取页面引用,如下所示:

Dim myReferer as string = Request.UrlReferrer

这将告诉您页面的来源。然后你可以拆分该字符串并进行比较。

另一种可能更好的方法是使用重定向传递查询字符串。然后,当您的页面加载时,检查该查询字符串。如果它不存在,请重定向页面或显示错误。

使用查询字符串重定向的示例:

Response.Redirect("myPage.aspx?myId=SomeId")

然后检查myPage中的字符串:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
     myString as string =  Request.QueryString("myId")

end Sub

答案 1 :(得分:0)

您可以创建第二条规则,将试图访问www.sitea.com/siteb/的人重定向到www.siteb.com

看起来像是:

<rules>
    <rule name="Rewrite to siteb.com" stopProcessing="true">
        <match url="^siteb/" negate="true" />
        <conditions>
            <add input="{HTTP_HOST}" pattern="^(www\.)?siteb\.com$" />
        </conditions>
        <action type="Rewrite" url="/siteb/" />
    </rule>
    <rule name="Prevent direct access">
        <match url="^siteb/" />
        <conditions>
            <add input="{HTTP_HOST}" pattern="^(www\.)?sitea\.com" />
        </conditions>
        <action type="Redirect" url="http://siteb.com" />
    </rule>
</rules>

我更改了Rewrite规则以缩短规则 Redirect默认发送301(永久) 每个试图直接访问www.sitea.com/siteb/的人都会被重定向到www.siteb.com,自己会重写第一个www.sitea.com/siteb/