简体   繁体   English

根据域名服务特定页面

[英]Serve specific page according to domain name

We have a clients domain name (d1) pointing at one of our sites (s1) which is a .NET 4 web forms site. 我们有一个客户域名(d1)指向我们的一个站点(s1),它是.NET 4 Web窗体站点。 The client has set up a subdomain on a different domain (d2) and pointed this at the s1 IP address. 客户端在另一个域(d2)上设置了一个子域,并将其指向s1 IP地址。 We need to serve a specific page on s1 if the d2 domain is used and not have the page in the URL. 如果使用d2域并且URL中没有该页面,则需要在s1上提供特定页面。 I would like to achieve this without a redirect if possible. 我想在没有重定向的情况下实现这一目标。

eg 例如

example.com -> the site example.com->网站

x.example.net -> the site /thepage.aspx (but want the URL in the address bar to remain x.example.net, not x.example.net/thepage.aspx). x.example.net->网站/thepage.aspx(但希望地址栏中的URL保持x.example.net,而不是x.example.net/thepage.aspx)。

I've tried doing a Server.Transfer in begin request and while this worked, the postback didn't (I assume because it's because of the transfer but I don't know how to detect a postback in begin request and thus not transfer). 我试过在begin请求中执行Server.Transfer,而在此过程中,回发没有进行(我想是因为这是因为传输,但我不知道如何在begin请求中检测回发,因此无法传输) 。

I thought there may be a way to leverage routing but there would be no path (just the domain name) so any route set up like this would presumably route all requests to this page if they don't get caught be a previous route - not ideal). 我认为可能有一种利用路由的方法,但是没有路径(只是域名),因此任何设置的路由都可能会将所有请求路由到该页面(如果它们没有被以前的路由捕获)-不是理想)。

So, in short: Is there a way to detect a postback in Application_BeginRequest in global.asax so I only transfer the inital request? 因此,简而言之:有没有一种方法可以在global.asax中的Application_BeginRequest中检测回发,因此我仅传输初始请求? Or is there a way of mapping a domain name to a page without redirecting? 还是有一种无需重定向即可将域名映射到页面的方法? Is there some feature I don't know about that achieves this? 有一些我不知道的功能可以实现这一目标吗?

You could write a HttpModule which examines incoming requests - if a request is for x.domain2.com, then you could invoke thepage.aspx like this: 您可以编写一个HttpModule来检查传入的请求-如果请求是针对x.domain2.com的,则可以这样调用thepage.aspx:

Type page_type = BuildManager.GetCompiledType ("~/thepage.aspx");
Page page = (Page) Activator.CreateInstance (page_type);
page.ProcessRequest (Context);

You can set up Rewrite Rules to do this. 您可以设置重写规则来执行此操作。 The following rule rewrites the root Url to /thepage.aspx only if the host matches x.example.net. 仅当主机匹配x.example.net时,以下规则才将根Url重写为/thepage.aspx。

RewriteCond  %{HTTP_HOST} (^x.example.net$)
RewriteRule ^$ / [NC,L]

If you have IIS7 : you can do this using URL Rewrite . 如果您有IIS7 :可以使用URL Rewrite进行

网址改写

If you have IIS6 : you can set up ISAPI Rewrite on the server. 如果您具有IIS6 ,则可以在服务器上设置ISAPI Rewrite

Depending on your setup, the 2nd line may include a slash: 根据您的设置,第二行可能包含斜杠:

RewriteRule ^/$ /thepage.aspx [NC,L] RewriteRule ^ / $ /thepage.aspx [NC,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM