如何在asp.net中将URL重写为子域,而无需在服务器上实际创建子域 [英] How to rewrite URL as subdomain in asp.net without actually creating a subdomain on server

查看:22
本文介绍了如何在asp.net中将URL重写为子域,而无需在服务器上实际创建子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这不是我第一次在 SO 上问这个问题.

I hope this isn't the first time i'm asking this question on SO.

我的网站中有 URL 并且使用了 query string 值例如:http://foo.com/xyzPage.aspx?barvalue=yehaa

I've URLs in my website and which are using query string values For example: http://foo.com/xyzPage.aspx?barvalue=yehaa

http://yehaa.foo.com/

请建议如何无需在服务器上实际创建子域 ..

Please suggest how it can be accomplished without actually creating subdomains on server ..

我在服务器机器上安装了 IIS 7.5 并使用 Asp.net 4.0.

I've IIS 7.5 installed on server machine and using Asp.net 4.0.

非常感谢

推荐答案

EDIT 按照我们的评论:

要使用 http://yehaa.foo.com/ 访问 http://foo.com/xyzPage.aspx?barvalue=yehaa,您必须使用以下规则:

To access http://foo.com/xyzPage.aspx?barvalue=yehaa using http://yehaa.foo.com/, you have to use the following rule:

<rules>
    <rule name="Rewrite subdomains">
        <match url="^/?$" />
        <conditions>
            <add input="{HTTP_HOST}" pattern="^(.+).foo.com$" />
        </conditions>
        <action type="Rewrite" url="http://foo.com?barvalue={C:1}" />
    </rule>
</rules>

它匹配每个 url 是否以 / 结尾并在 foo.com 之前使用一些东西,然后将其重写为 http://foo.com?barvalue={C:1} 其中 {C:1} 是在 foo.com 之前输入的任何值.

It matches every url ending or not with a / and using something before foo.com and then rewrites it to http://foo.com?barvalue={C:1} where {C:1} is whatever value was entered before foo.com.

如果你想阻止人们直接访问http://foo.com?barvalue={C:1},你可以使用下面的规则.

If you want to prevent people from accessing directly to http://foo.com?barvalue={C:1}, you can use the rule below.

您可以使用为 IIS 重写模块,方法是在您的 web.config 文件:

You could use the Rewrite module for IIS by adding the following rule in your web.config file:

<rewrite>
    <rules>
        <rule name="Redirect to Subdomains" stopProcessing="true">
            <match url="^xyzPage.aspx$" />
            <conditions>
                <add input="{QUERY_STRING}" pattern="^barvalue=(.+)$" />
            </conditions>
            <action type="Redirect" url="http://{C:1}.{HTTP_HOST}" appendQueryString="false" />
        </rule>
    </rules>
</rewrite>

它检查 url 是否与 xyzPage.aspx 完全匹配(之前或之后都没有).
它检查查询字符串是否包含 barvalue 参数(并且只有这个),以及它的值是否不为空.
如果这 2 个条件都可以,它会触发 Redirecthttp://barvalue.original.host.

It checks if the url matches exactly xyzPage.aspx (nothing before or after).
It checks if the querystring contains the barvalue parameter (and only this one) and if its value is not empty.
If those 2 conditions are ok, it triggers the Redirect to http://barvalue.original.host.

您的问题指定Rewrite,所以如果这确实是您想要做的,请将操作type="Redirect" 更改为type="Rewrite".

Your question specify Rewrite, so if this is really what you want to do, change the action type="Redirect" to type="Rewrite".

重要:您可能需要应用程序请求路由模块在启用代理模式的情况下安装和设置以重写到不同的域.

Important: you may need the Application Request Routing module installed and setup with the proxy mode enabled to Rewrite to a different domain.

这篇关于如何在asp.net中将URL重写为子域,而无需在服务器上实际创建子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆