Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS Rewrite Module and sub applications

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>
like image 658
MaxSC Avatar asked Jun 06 '13 14:06

MaxSC


People also ask

What is rewrite module IIS?

About the URL Rewrite module The Microsoft URL Rewrite Module 2.0 for IIS 7 and above enables IIS administrators to create powerful customized rules to map request URLs to friendly URLs that are easier for users to remember and easier for search engines to find.

What is the difference between rewrite and redirect?

Simply put, a redirect is a client-side request to have the web browser go to another URL. This means that the URL that you see in the browser will update to the new URL. A rewrite is a server-side rewrite of the URL before it's fully processed by IIS.

How do I know if IIS URL Rewrite module is installed?

To see if the URL Rewrite module is installed, open IIS Manager and look in the IIS group - if the module is installed, an icon named URL Rewrite will be present.


1 Answers

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

like image 131
cheesemacfly Avatar answered Oct 18 '22 17:10

cheesemacfly