문제

Let's say for example that my site is www.mysite.com.

I have create a sub domain here, let's say sub.mysite.com, and deployed an asp.net webform application in here. Everything works fine when I browse to http://sub.mysite.com

A friend of mine, who owns another website, let's say www.hissite.com, has created a DNS entry for the subdomain sub.hissite.com and setup the IP address of my web site. He want to redirect the subdomain of his web site to the subdomain of my website.

Unfortunately my provider told me that this kind of configuration works only for 2nd level domains and that I should create a piece of code, in my www.mysite.com web application, to check if the request is relative to the site sub.hissite.com, and do a redirect in that case to my sub.mysite.com.

How do I check if the request is for sub.hissite.com in my webform application? What happens to the URL in the client browser?

Thanks for help

EDIT 1: Actually I have configured an alias for my website but I get the following error when navigating to sub.hissite.com:

Application Request Routing Error 
500 - Internal server error. 
There is a problem with the resource you are looking for, and it cannot be displayed. The domain may not be inserted correctly on Rewrite Maps.

EDIT 2: Following the answer of OnoSendai I have implemented an MVC ActionFilter

public class MyRedirectFilter : ActionFilterAttribute
{
    public override void OnActionExecuting( ActionExecutingContext filterContext ) {
        string newUri = "http://sub.mysite.com";
        HttpRequestBase request = filterContext.HttpContext.Request;
        string referer = request.ServerVariables["HTTP_HOST"]; 
        if ( referer != null && referer.Contains( "hissite" ) ) {
            filterContext.HttpContext.Response.Redirect( newUri, true );
        }
    }
}

And now the application works nice. However the address in the browser address bar change from www.hissite.com to sub.mysite.com. Is there any way to make it not change? Thanks

도움이 되었습니까?

해결책

Basically what your ISP told you is that you'll need to do the check and redirect yourself.

One way to do that programatically is to check the HTTP_REFERER header, using Request.UrlReferrer or a direct reference via Request.ServerVariables - something like this:

if (Request.ServerVariables("HTTP_HOST") == "sub.hissite.com/page1.aspx")
    Response.Redirect("sub.mysite.com/page1.aspx");

Now you may be thinking, 'but wait - I'll have to implement this on all pages?'

Oh, heavens, no! You may try to bundle this check inside a neat HttpModule. That'll allow you to intercept all requests (including resource requests such as jpg, js and css files) and decide what to do - which is, basically, what the UrlRewrite module does.

A very simple and neat post about HttpModules can be found here:

http://blogs.msdn.com/b/alikl/archive/2007/12/26/basic-httpmodule-sample-plus-bonus-case-study-how-httomodule-saved-mission-critical-project-s-life.aspx

Hope it works for you.

다른 팁

The easier way would be to configure IIS to point to your site for that kind of request but as you say in comments you can't because you are in a shared hosting without access to IIS.

Than an easy solution could be to Use Asp.net Routing. Anyway you need the routing module to be installed on server.

In web.config:

<system.webServer>
...
<rules>
    <rule name="Rewrite subdomains">
        <match url="(.*)" />
        <conditions>
             <!--take all request for any subdomain of hissite.com-->
            <add input="{HTTP_HOST}" pattern="^(.+)\.hissite\.com$"  ignoreCase="true"/>
        </conditions>
        <!--rewrite request as sub.mysite.com-->
        <action type="Rewrite" url="http://sub.mysite.com/{R:0}" />
    </rule>
</rules>
</system.webServer>

I cannot test this so maybe you need to adjust to your needs.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top