6

Here's what I have deployed:

IIS deploy

testRedirect is an empty website. All sub-applications are sub-folders that have been converted in application. All of them are ASP .Net MVC sites.

Here's what I want to setup:

  • Http://localhost/ must show the content of SiteName1 without displaying Http://localhost/SiteName1/ in the adress bar (it must stay Http://localhost/)

  • Http://localhost/SiteName1/ must show the content of SiteName1 without displaying Http://localhost/SiteName1/ in the adress bar (it must stay Http://localhost/)

  • Http://localhost/SiteName2/ shows the content of SiteName2 and displays Http://localhost/SiteName2/ in the adress bar (Same behavior for SiteName3 & SiteName4 and any other sites....)

In other words, I want my SiteName1 to act like a home site

What I've tried so far, is something similar to the answer provided by @cheesemacfly here:

<rules>
    <rule name="Redirect if SiteName1" stopProcessing="true">
        <match url="^SiteName1/(.*)$" />
        <action type="Redirect" url="{R:1}" />
    </rule>
    <rule name="Rewrite to sub folder">
        <match url="^.*$" />
        <action type="Rewrite" url="SiteName1/{R:0}" />
    </rule>
</rules>

It works great for Case1 & 2 but not the other ones.

I tried to add rules like this one, but it was not successful...

<rule name="if_not_SiteName1" stopProcessing="true">
   <match url="^SiteName1/(.*)$" negate="true" />
   <action type="None" />
</rule>
Community
  • 1
  • 1
MaxSC
  • 4,698
  • 2
  • 23
  • 36
  • Are you using any routing system? Because if not, you may be able to use the `IsFile/IsDirectory` condition (so if the requested path, let's assume `http://localhost/SiteName2/default.aspx` or `http://localhost/SiteName2/`, points to a real file/folder, then you don't trigger the rule). Would it work in your case? – cheesemacfly Jun 06 '13 at 17:07
  • @cheesemacfly I do use routing, these are ASP .Net MVC sites. I've updated my question. – MaxSC Jun 07 '13 at 08:57

1 Answers1

4

I think your best option would be to trigger the rewrite rule you already have only when the url doesn't start with one of your sub-applications.

It would be something like:

<rules>
    <rule name="Redirect if SiteName1" stopProcessing="true">
        <match url="^SiteName1/(.*)$" />
        <action type="Redirect" url="{R:1}" />
    </rule>
    <rule name="Rewrite to sub folder">
        <match url="^(SiteName2|SiteName3|SiteName4)/" negate="true" />
        <action type="Rewrite" url="SiteName1/{R:0}" />
    </rule>
</rules>

We keep the redirect when SiteName1/ is requested (we don't need to change this), but the rewrite rule is triggered only when the requested url doesn't start with SiteName2/ or SiteName3/ or SiteName4/ (that's what url="^(SiteName2|SiteName3|SiteName4)/" means and we use negate="true" to triggered the rule only when the pattern is not matched).

cheesemacfly
  • 11,622
  • 11
  • 53
  • 72
  • Thanks, it does the trick! But would it be possible not to have to specify `(SiteName2|SiteName3|SiteName4)` and just create rule based on `SiteName1` only? I'll have to deal with different sets of SiteNames (sometimes just `SiteName2` & `SiteName4`, sometimes `2`, `3` and `5`) depending on the webserver. I want to avoid to write specific rulesets for different webserver farms as the only common thing will always be the `SiteName1` rule. Thanks! – MaxSC Jun 07 '13 at 14:28
  • I see your point, I will try to think of a solution and edit my answer if I find a good one :) – cheesemacfly Jun 07 '13 at 15:13
  • I can't think of a more simple way than using the negate option. Maybe those sub-applications are following a pattern? Like `SiteName[0-9]` or something the same kind? – cheesemacfly Jun 08 '13 at 02:17
  • 1
    Unfortunately, no. It would have been too easy! :-) Anyway, thanks for your help! – MaxSC Jun 08 '13 at 07:23