I have the next configuration of websites in the IIS:
http://main.domain.com
website (ASP.NET MVC 5 app, website binded to the specified host, port 80)- default website binded to :80 with ARR/URL Rewrite module enabled
A wildcard binding *.domain.com
is specified in DNS settings.
The desired behavior is to have http://main.domain.com
as an entry point and a set of dynamic user-subdomains like http://user1.domain.com
, http://user2.domain.com
, etc.
Now this behavior is simulated using links like http://main.domain.com/user/user1
I have set up the URL Rewrite rule for main.domain.com
in the way like this:
<rule name="user-redirection" enabled="true" stopProcessing="true">
<match url="^.*$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="^([\w\d-]+)\.domain\.com$" />
</conditions>
<action type="Rewrite" url="http://main.domain.com/user/{C:1}/{R:0}" logRewrittenUrl="true" />
</rule>
Everything is ok here - I can see that http://user1.domain.com
works just like http://main.domain.com/user/user1
as it worked earlier.
Then I try to re-implement a logic of checking the existance of specified user in the database. For instance, when user47 doesn't exist - opening of the http://main.domain.com/user/user47
link leads to redirection to the http://main.domain.com
entry point.
In the code-side it is done by adding custom filter attribute to the controller action that implements the needed conditional redirect. I have the next code:
public class UserController : Controller {
[CustomRedirectBehavior]
public ActionResult Index()
{
...
}
}
public class CustomRedirectBehaviorAttribute : ActionFilterAttribute {
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
...
if (redirectingCondition) {
filterContext.Result = new RedirectResult("http://main.domain.com");
}
}
}
And here.... I get a cyclic redirection error in the browser! To be certain - I have double-checked this behavior:
- When I open
http://main.domain.com/user/user47
I'm properly redirected tohttp://main.domain.com
- When I open
http://user47.domain.com/
I get a cyclic redirection error.
Then in order to investigate the problem I've modified redirect callback to:
filterContext.Result = new RedirectResult("http://someotherdomain.com/some/other/path");
And.... I can see that:
- When I open
http://main.domain.com/user/user47
I'm properly redirected tohttp://someotherdomain.com/some/other/path
- When I open
http://user47.domain.com/
I'm redirected tohttp://user47.domain.com/some/other/path
!!! (And yes, that's not a typo and I've also double-checked this behavior)
So. I need an idea on how to pass through this problem