II7 Rewrite Issues - excluding pages - url

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.

Related

URL Rewrite match question mark

I am trying to match a ? in my url rewrite section in my web config, I am using IIS Express so I can not add the rule via IIS manager.
<rules>
<rule name="LocationDetail" stopProcessing="true">
<match url="^location-detail\?province=([A-Za-z]{2})$" />
<action type="Redirect" url="LocationDetail"/>
</rule>
</rules>
As you can see I am already escaping the ?. Is there something I am missing?
I also modify the url so that the the question mark is not needed to see if the question mark is the reason it was not matching.
<rules>
<rule name="LocationDetail" stopProcessing="true">
<match url="^location-detailprovince=([A-Za-z]{2})$" />
<action type="Redirect" url="LocationDetail"/>
</rule>
</rules>
It was. Once the question mark was removed it matched correctly and I was redirected correctly.

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.

URL Rewrite two different scenarios

I want to achieve the below:
When the URL is https://www.example.com/ it should be redirected to https://www.otherdomain.com/
When the URL is https://www.example.com/images/anything, it should stay in the same domain https://www.example.com/images/anything.
How can I do this?
I got the following for 1:
<rule name="Rule1">
<match url=".*"/>
<conditions>
<add input="{HTTP_HOST}" pattern="https://www.example.com/"/>
</conditions>
<action type="Rewrite" url="https://www.otherdomain.com/>
</rule>
How to create a rule for point 2?

IIS Rewrite Module and sub applications

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

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}

Resources