IIS rewrite virtual folder - url

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}

Related

Issue with URL Rewriting - IIS & MVC

I trying to build a rewrite rule to "Permanent Redirect" of a PHP page to MVC site. here is the URL of PHP site
/Home/new-age-xxx.php & url of MVC site to which I want user to redirect "/home/new-age-xxx"
here is my URL rewriting rule
<rule name="Imported Rule 1-599" stopProcessing="true">
<match url="^home\new/-age/-xxx\.php$" ignoreCase="false" />
<action type="Redirect" url="/home/new-age-xxx" appendQueryString="false" redirectType="Permanent" />
</rule>
It is not redirecting to /home/new-age-xxx, even i tried to move it to other urls like /login/index
Any suggestion?
Thanks
You could use a bewlo url rewrite rule to redirect one site url to another site:
e.g:
Request URL: http://www.sample2.com/home/new-age-123.php
Redirect url:http://www.example.com/home/new-age-123
<rule name="reg redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_URI}" pattern="/home/new\-age\-([0-9]{1,3}).php$" />
</conditions>
<action type="Redirect" url="http://www.example.com/home/new-age-{C:1}" appendQueryString="false" />
</rule>
To learn more about url rewrite module you could refer the bewlo link:
Creating Rewrite Rules for the URL Rewrite Module
Regards,
Jalpa

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 Rewrite multiple parameters

I am really confused about the URL rewrite interface. I dont understand what i need to do.
I have an url as:
www.example.com/diretory/subdirectory/index.html?param1=1&param2=2&param3=2&param4=7
I want to hide this url in a <a>-href tag, which displays "Example Tag".
When the url is requested it should rewrite it to
www.example.com/program/location/year/vacancie
I allready tried this:
<system.webServer>
<rewrite>
<rules>
<rule name="ProgramRewrite" patternSyntax="ECMAScript" stopProcessing="true">
<match url="\?([^/]+)&([^/]+)&([^/]+)&([^/]+)" />
<action type="Rewrite" url="www.example.com/program/location/year/vacancie" logRewrittenUrl="true" />
<conditions>
</conditions>
</rule>
</rules>
</rewrite>
</system.webServer>
In the URL Rewrite Interface the Test Pattern said it is working and gets:
?param1=1&param2=2&param3=2&param4=7
param1=1
param2=2
param3=2
param4=7
I checked the log url rewrite as well, but in my logs it is not shown.
2017-03-20 16:29:24 192.168.253.146 GET /diretory/subdirectory/index.html param1=1&param2=2&param3=2&param4=7 88 - 192.168.253.146 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/56.0.2924.87+Safari/537.36 - 304 0 0 4
ps: the urls are not working and only for illustrative purpose.
The match URL only matches the URL and does not take the querystring into account. You will need to add a condition for this. Also do you want to rewrite this (so the server internally executes the new URL) or redirect (so the server will request the browser to go to the new URL and URL changes in address bar). In case you want to rewrite you should not add the domain again, in case you want to redirect add http:// as well. Assuming a rewrite is what you want use below rule:
<rule name="ProgramRewrite" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="/program/location/year/vacancie" logRewrittenUrl="true" />
<conditions>
<add input="{QUERY_STRING}" pattern="([^/]+)&([^/]+)&([^/]+)&([^/]+)" />
</conditions>
</rule>

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

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