简体   繁体   中英

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. The client has set up a subdomain on a different domain (d2) and pointed this at the s1 IP address. We need to serve a specific page on s1 if the d2 domain is used and not have the page in the URL. I would like to achieve this without a redirect if possible.

eg

example.com -> the site

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).

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).

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? 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:

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.

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

If you have IIS7 : you can do this using URL Rewrite .

网址改写

If you have IIS6 : you can set up ISAPI Rewrite on the server.

Depending on your setup, the 2nd line may include a slash:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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