How to rewrite URL as subdomain in asp.net without actually creating a subdomain on server

☆樱花仙子☆ 提交于 2019-11-27 23:16:32

EDIT following our comments:

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>

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.

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


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>

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!