Adding a "fake" root folder using rewrite - iis

I'm not sure where to go on this one. I've got rewrites to remove file extensions and such but what I can't seem to find out how to do is to add a "fake" root directory to the site. For example if the site is www.foo.com/index.htm I'd like to rewrite the URL to show www.foo.com/root/index.htm. I can use either the IIS rewrite module or mod rewrite I'm just uncertain on how to go about this (or if it's even possible) and my google-fu has failed me.

If I understand it correctly, you want requests to come with root prefix. In that case this rewrite will do it:
<rule name="StripRoot" stopProcessing="true">
<match url="root/(.*)" />
<action type="Rewrite" url="/{R:1}" />
</rule>
To modify the html served to the client outbound rule is required:
<outboundRules>
<rule name="FakeRoot" preCondition="IsHtml">
<match filterByTags="A, Area, Base, Form, Frame, IFrame, Img, Input, Link, Script" pattern="http://www\.foo\.com/(.*)" />
<action type="Rewrite" value="http://www.foo.com/root/{R:1}" />
</rule>
<preConditions>
<preCondition name="IsHtml">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
It assumes that you have fully qualified URL in tag attributes, if relative links are used you need more sophisticated rewrites.

Related

Keycloak behind IIS Reverse Proxy

I just cant get my reverse proxy to work properly and I hope that someone can help me with that.
A few Informations:
1 VM Windows Server 2019,IIS 10 (with ARR and URL Rewrite Module),"keycloak" App under the "Default Website", proxy-address-forwarding="true" were added in the standalone.xml
keycloak is locally available at "localhost:8080". The default website is available from intern network (Port 80 is open, 8080 is closed). I want to use the IIS as a reverse proxy for keycloak. It should look like the following URL.
Request: "http://server_fqdn/keycloak" -> IIS Reverse Proxy -> localhost:8080
At first I tried it without "/keycloak" and it worked perfectly. After I added "/keycloak" it just shows 404 Errors. I saw that it tries to open "http://server_fqdn/auth" instead of "http://server_fqdn/keycloak/auth". If I enter "/keycloak/auth" manually it works. My first thought was to just write another Blank Inbound Rule which redirects "http://server_fqdn/auth" to "http://server_fqdn/keycloak/auth". It works, but now there is another problem. If i want to enter the admin console I get an error which says "Invalid parameter: redirect_uri".
It stops at the following URL (not complete, but the necessary part is there)
http://server_fqdn/keycloak/auth/realms/master/protocol/openid-connect/auth?client_id=security-admin-console&redirect_uri=http%3A%2F%2Fserver_fqdn%2Fkeycloak%2Fauth%2Fadmin%2Fmaster%2Fconsole%2F&state=.....
If i remove %2Fkeycloak after "redirect_uri" it works and i get the keycloak login screen. Maybe someone can help me here.
Inbound Reverse Proxy Rule:
<rule name="ReverseProxyInboundRule1" enabled="true" stopProcessing="true">
<match url="^keycloak/(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="http://localhost:8080/{R:1}" />
<serverVariables>
</serverVariables>
</rule>
Outbound Reverse Proxy Rule:
<outboundRules>
<clear />
<rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1" enabled="true">
<match filterByTags="A, Area, Base, Form, Head, IFrame, Img, Input, Link, Script" pattern="^http://localhost:8080/(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
</conditions>
<action type="Rewrite" value="http://server_fqdn/keycloak/{R:1}" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
Inbound Reverse Proxy Rule:
<rule name="keycloak" enabled="true" stopProcessing="true">
<match url="^auth/(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="http://server_fqdn/keycloak/{R:0}" redirectType="Permanent" />
</rule>

IIS Rewrite causing redirect issues

I have the following rules in IIS.
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://localhost:3000/{R:1}" />
</rule>
</rules>
My web application internally calls another domain www.anotherdomain.com/somepage. My problem is while it redirects, it changes the url to www.mycurrentdomain.com/somepage which obviously results in a 404 page. Can some one please help me to write the correct rule.
Note - the redirect is happening from the application code res.redirect('www.anotherdomain.com/somepage')
We need to handle outbound rules to apply the URL rewrite rule rather than use inbound rules. Here is an example of stopping the internally call redirecting to outside domain(https://www.bing.com). You can change it according to your actual needs.
<system.webServer>
<rewrite>
<outboundRules>
<rule name="MyLocationRule" preCondition="IsRedirection">
<match serverVariable="RESPONSE_Location" pattern="https://www.bing.com/" />
<action type="Rewrite" value="http://{HTTP_HOST}/webform2.aspx" />
</rule>
<preConditions>
<preCondition name="IsRedirection">
<!--301,302 Url redirection http status code.-->
<add input="{RESPONSE_STATUS}" pattern="3\d\d" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
</system.webServer>
Feel free to let me know if the problem still exists.

IIS Rewrite rule, url modify himself

I have write a rewrite rule to redirect an incoming request to the correct server.
Here my web.config :
<rules>
<rule name="ToMonceau">
<match url="test/(.*)" />
<action type="Rewrite" url="http://10.5.5.83/{R:1}" />
<serverVariables>
<set name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" value="{HTTP_ACCEPT_ENCODING}" />
<set name="HTTP_ACCEPT_ENCODING" value="" />
</serverVariables>
</rule>
</rules>
<outboundRules>
<rule name="AddPrefix" preCondition="IsText" enabled="true">
<match filterByTags="A, Img, Link, Script" pattern="(http://10.5.5.83/mantis/)?(/mantis/)?(.*)" />
<conditions>
<add input="{URL}" pattern="(test/mantis)/(.*)" />
</conditions>
<action type="Rewrite" value="./{R:3}" />
</rule>
<rule name="RestoreAcceptEncofing" preCondition="NeedsRestoringAcceptEncoding">
<match serverVariable="HTTP_ACCEPT_ENCODING" pattern="^(.*)" />
<action type="Rewrite" value="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" />
</rule>
<preConditions>
<preCondition name="IsText">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/(.+)" />
</preCondition>
<preCondition name="NeedsRestoringAcceptEncoding">
<add input="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" pattern=".*" />
</preCondition>
</preConditions>
</outboundRules>
My problem when I go to http://localhost/test/mantis (my server hosts a mantis here), the url change automatically to http://localhost/mantis/, I have to put again the "test/" and it change to http://localhost/mantis/login_page.php. I reput again "test/" and this time the login's page of mantis shown.
The problem continue when I try to log in, the url changes continously by removing the "test/" part.
If I go directly to my mantis without the redirect (http://10.5.5.83/mantis) everything is working like a charm.
What am I missing in my rule to do it correctly ?
I take the exemple here with mantis but I have the same problem on every sites host by different server.
Thanks in advance and sorry if my english is not perfect.
Go to IIS manager and click on URL rewrite:
Keep pattern .*
Action type should be Rewrite
Rewrite URL: http://yourdesiredurl/{R:0}

Scope IIS Rewrite Outbound Rule by request URL

Is there any way to scope IIS' outbound rewrite rule to only apply if the requested URL matches a pattern? I suspect that's not possible because the request URL isn't visible in the response, but I thought I'd ask in hopes that it is possible.
Here is my current outbound rule:
<rewrite>
<outboundRules>
<rule name="Change Absolute Paths" preCondition="ResponseIsHtml1">
<match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^http(s)?://blog.mycompany.com/blog(.*)$" />
<action type="Rewrite" value="https://www.mycompany.com/blog{R:2}" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
It would be great if I can add a condition in there so that the outbound rule only applies if the request URL matches a pattern.
It turns out to be easy. You just need to add a condition on {REQUEST_URI}.
<rewrite>
<outboundRules>
<rule name="Change Absolute Paths" preCondition="ResponseIsHtml1">
<match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^http(s)?://blog.mycompany.com/blog(.*)$" />
<conditions>
<add input="{REQUEST_URI}" pattern="^/foobar" />
</conditions>
<action type="Rewrite" value="https://www.mycompany.com/blog{R:2}" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>

static url rewrite, using rewrite map is not working

I have a website on azure, and I am trying to make some user-friendly urls to my websites use.
I have downloaded IIS remote manager, and did as it was explained here.
Although the article is since 2008, the GUI of the remote IIS manager is still almost the same.
after defining the rewrite map, and the rule that looks inside the map, this is the web.config that was generated:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<outboundRules>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
<rewriteMaps>
<rewriteMap name="StaticRewrites">
<add key="/niceurlpart" value="/Menu/master/#/Menu?Token=7926983e-c64e-4547-85f5-d85e3c06c7a8" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="Rewrite rule1 for StaticRewrites" patternSyntax="ExactMatch">
<match url=".*" />
<conditions>
<add input="{StaticRewrites:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
when I enter inside the address bar: mydomain.com/niceurlpart I get:
"The resource you are looking for has been removed, had its name changed, or is temporarily unavailable."
Also, when I try to test the pattern inside the remote IIS manager, it fails..
am I missing something?
Not sure on the syntax of doing this in web.config, but the # part of a URL doesn't reach the server, so using that in your rule won't work.
If you are using angular type app and are using # for routing, then try using HTML5 mode which will get rid of the hash.
If your are after SEO, you can google '_escaped_fragment_' to get the hash value to your server. Also if SEO is your goal then we use _escaped_fragment_, along with this tool https://prerender.io

Resources