IIS Rewrite Module and sub applications - url

Here's what I have deployed:
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>

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

Related

IIS Rewrite and reverse proxy for multiple applications

I have an IIS 10 with a website configured on that. There are multiple Applications underneath that website.
MyWebsite/app1 - MyWebsite/app2 - MyWebsite/app3
I Have another server(let's call it EndServer) hosting 3 websites on 3 different ports.
well, now what I wanna do is using IIS as a reverse proxy to redirect and MASK the application 1 to one of those websites in 2nd server and application 2 to another one.
at the end, Users will enter https://mywebsite/app1 and they will see the contents of website 1 in the Endserver.
Note: it is important for me that end Users see the URL like as https://mywebsite/app1/
how shall I edit the Rule below:
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://endserver:8052/{R:1}" />
<conditions trackAllCaptures="true">
</conditions>
</rule>
</rewrite>
Thanks
A.
According to your description, I suggest you could try to use below url rewrite rule.
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{URL}" pattern="^/app1/(.*)" />
</conditions>
<action type="Rewrite" url="http://endserver:8052/{C:1}" />
</rule>
<rule name="ReverseProxyInboundRule2" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{URL}" pattern="^/app2/(.*)" />
</conditions>
<action type="Rewrite" url="http://endserver:8053/{C:1}" />
</rule>
Thanks for your answer.
I got same error. as you see below /App1 is missing in the URL after localhost:Port
localhost:8888/assets/styles/Custom.css?m=1549903616.0
I believe the trouble is with rewriting the response URL. I don't know who can I add the missing part in URL.
Regards.

IIS URL redirection - igonre match all for some URLs

I had to change previous URL pattern to something else. Earlier URL pattern was
www.testdomain.com/hotels/"city" and it changed to www.testdomain.com/hotel-"city"
Below is the redirection I have used that for.
<rule name="cityChange" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^hotels/(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="www.testdomain.com" />
</conditions>
<action type="Redirect" url="www.testdomain.com/hotel-{R:1}"appendQueryString="false" redirectType="Temporary" />
</rule>
Now I have another issue which is another URL I have used for the site is also redirecting to different one. That URL is,
www.testdomain.com/hotels/'city'/'name'-'street'-'postalcode'
Since I have used a wildcard for /hotels/'city' to /hotel-'city' it changes above URL as
www.testdomain.com/hotel-'city'/'name'-'street'-'postalcode'
as well. But that URL is not valid in the site and I do not want to replace "/" from "-" there.
How can I exclude this URL pattern from that wild card which I have mentioned above?
Thanks in advance.
Can't be sure about that but did you try to add parentheses, like that ?
<match url="^(hotels)/(.*)" />
We did that for ou specific wildcard redirect :
<rule name="modeles-en" enabled="true" stopProcessing="true">
<match url="^(modeles-en)/(.*)" ignoreCase="true" />
<action type="Redirect" url="/models/{R:2}" redirectType="Permanent" />
</rule>
Hope it helps !

Globally modify left part URL in MVC 4 / 5

I am using IIS 8.5 with MVC4 and MVC5.
Let´s assume that I have two websites where the URL of the first (WS1) is:
http://myapp.example.com
and the second website (WS2) URL is:
http://myapp.example.com/sub
(and is located on another port to which I want to forward)
Now, my problem is that all URLs generated by MVC (links etc) will use the left part of the URL and remove /sub within that application. Let´s say for instance that in WS2 I want to redirect the user to http://myapp.example.com/sub/login but it will become http://myapp.example.com/login instead.
I have tried IIS URL rewrites but it will only work first-time request that actually contain the /sub part, the URLs generated by MVC of course remains erroneously without the /sub part.
<rewrite>
<rules>
<rule name="Rewrite">
<match url="^Sub/(.*)" />
<action type="Rewrite" url="http://myapp.example.com/{R:1}" />
</rule>
</rules>
<outboundRules>
<rule name="Response Status Update" preCondition="ResponseStatus" stopProcessing="true">
<match serverVariable="RESPONSE_Location" pattern="^/(.*)" />
<action type="Rewrite" value="/Sub/{R:1}" />
</rule>
<rule name="RewriteRelativePaths" preCondition="IsHTML">
<match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^/(.*)" negate="false" />
<conditions>
<add input="{URL}" pattern="^/Sub/.*" />
</conditions>
<action type="Rewrite" value="/Sub/{R:1}" />
</rule>
<preConditions>
<preCondition name="IsHTML">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
<preCondition name="ResponseStatus">
<add input="{RESPONSE_STATUS}" pattern="3[0-9][0-9]" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
My question is how I can accomplish this. Using another subdomain is not an option.

IIS rewrite virtual folder

I need to create a URL rewrite rule in IIS for the following:
From:
http://hostname/virtual_path_folder/myisapi.dll?a=1&b=1
To:
http://hostname/myisapi.dll?a=1&b=1
Basically, I'd just like to hide the virtual_path folder if possible.
You could go with the 2 following rules:
<rules>
<rule name="Redirect if virtual_path_folder" stopProcessing="true">
<match url="^virtual_path_folder/(.*)$" />
<action type="Redirect" url="{R:1}" />
</rule>
<rule name="Rewrite to sub folder">
<match url="^.*$" />
<action type="Rewrite" url="virtual_path_folder/{R:0}" />
</rule>
</rules>
The first one, Redirect if virtual_path_folder, will redirect every request starting with virtual_path_folder/. It will prevent anyone from accessing your content using the sub folder.
The second one rewrites any request (^.*$) to the sub folder: virtual_path_folder/{R:0}

II7 Rewrite Issues - excluding pages

Hey all, I've got a question about
IIS7 rewrite.
I'm wondering if there is a way to set
conditions for re writing urls. I'm
wanting to rewrite:
http://www.domain.com/user.aspx?id=username
to become
http://www.domain.com/username/
I also have
http://www.domain.com/article.aspx
http://www.domain.com/login.aspx
and I want those to become
http://www.domain.com/article/
http://www.domain.com/login/
The issue I'm having is that if I set
up the rewrites for username the
rewrites for article and login break.
I need to somehow exclude those from
the username rewriting so that they
can be handled on their own.
Here is my current code, im rewriting
the usernames to /user/username at the
moment:
<rewrite>
<rules>
<rule name="Rewrite user accounts2">
<match url="user/([_0-9a-z-]+)"/>
<action type="Rewrite" url="user.aspx?id={R:1}"/>
</rule>
<rule name="Rewrite user accounts">
<match url="user/([_0-9a-z-]+)/"/>
<action type="Rewrite" url="user.aspx?id={R:1}"/>
</rule>
</rules>
</rewrite>
If you add the article/login rule at the top and add stopProcessing="true" to it then you dont have to use conditions.
<rewrite>
<rules>
<rule name="Login" stopProcessing="true">
<match url="login/?"/>
<action type="Rewrite" url="login.aspx"/>
</rule>
<rule name="Article" stopProcessing="true">
<match url="article/?"/>
<action type="Rewrite" url="article.aspx"/>
</rule>
<rule name="Rewrite user accounts2" stopProcessing="true">
<match url="user/([_0-9a-z-]+)/?"/>
<action type="Rewrite" url="user.aspx?id={R:1}"/>
</rule>
</rules>
</rewrite>
PS. the questionmark makes the preceding character optional.

Resources