Owin URL Rewrite to different port - asp.net

We have a series of smaller APIs part of a larger system, each running in their own sites on an IIS machine.
To hide our internal architecture, we've created a series of IIS rewrite rules:
<rule name="AuthServer" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^auth/.*" />
<action type="Rewrite" url="https://localhost:4000/{R:0}" />
</rule>
However, we need to extend this system and make it testable.
We've tried:
if (context.Request.Uri.AbsoluteUri.EndsWith("test"))
{
context.Request.Path = new PathString("/rewritten");
}
But this only allows us to change the path. We need to rewrite to a different port/site.
Can this be done?

We didn't find a way to do this, so we ended up using:
https://msdn.microsoft.com/en-us/library/microsoft.web.administration.servermanager.getwebconfiguration(v=vs.90).aspx
To dynamically update the web.config rewrite rules from code. It works fine, and is testable (though not locally hosted).

Related

Reverse proxy Kibana 7 with ARR in IIS

I set up some days ago an ELK 7 stack to monitor some logs in one place. For now it perfectly operational and works well from inside the network. I now want to access Kibana from outside the network without the need of a VPN.
I already have a website running in IIS which is served through a global reverse proxy. The website itself is doing nothing but there are 2 WebApplications under it (there's just a little app at the webiste root doing a redirect to the right app depending of the source of the request).
So at the moment, i have the following URLs available (servername could be either my machine name when i'm on the intranet or the external domain when i'm remoting) :
http(s)://servername/app1
http(s)://servername/app2
Now i would like to be able to get the following URL working to access Kibana
http(s)://servername/elk
For now i didn't manage to make it work - even internally - (so without involving the first global reverse proxy).
I set up a application under a website dedicated for testing purpose (so it won't mess with the real website). This is an extract of my web.config file for this application :
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="KibanaProxy">
<match url="(.*)" />
<action type="Rewrite" url="http://kibanahost:5601/{R:1}" />
</rule>
</rules>
<outboundRules>
<rule name="KibanaRedirect" preCondition="Redirect">
<match serverVariable="RESPONSE_Location" pattern="^/(.*)" />
<action type="Rewrite" value="/elk/{R:1}" />
</rule>
<rule name="KibanaContent" preCondition="ResponseIsHtml1">
<match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Input, Link, Script" pattern="^/(.*)" />
<action type="Rewrite" value="/elk/{R:1}" />
</rule>
</outboundRules>
</rewrite>
</system.webServer>
</configuration>
I wrote a rule to handle the first redirect done when accessing the Kibana root and i tried to add a rewrite rule so external resources can be accessed also. It seems like i have still some other rewrites to do mainly because of URL inside bootstrap.js and such things like CSS... It seems like it's a pain to achieve and i wonder if anyone managed to accomplish such a proxying. If i can proxy Kibana correctly internally, i would be able to add an authentication layer thanks to IIS before trying to expose it over the internet through the global reverse proxy.

web.config redirect not working on localhost

I have a website that I work on in visual studio and then deploy to azure. I'd like to create a permanent redirect in my web.config file. I would like to test this to make sure it works before I deploy to azure.
Here is my web.config file
<rewrite>
<rules>
<rule name="301Redirect" stopProcessing="true">
<match url=".*/PEOs.aspx" />
<action type="Redirect" url="http://myblogpost.com" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
So when the user visits www.mysite.com/PEOs.aspx they will be directed to my blog post. The problem is, when I test this, it just goes to the page. Doesn't redirect. I realize I could just write some code in the code behind file, but I want something that's more SEO friendly.
Am I doing something wrong here? I'm fairly certain regex can be used here. But when I replaced it with http://localhost:21537/PEOs.aspx it doesn't work either. I don't want to publish to azure unless I know that this will only be an issue during local testing.
Try using: <match url="PEOs.aspx" />
I was not entirely sure so I checked it here: https://support.rackspace.com/how-to/rewrite-urls-from-aspnet-on-cloud-sites/

Why does a reverse proxy rewrite rule stop working when System.Web.Routing is active?

Boiling this down to the simplest possible rule:
<rule name="Reverse Proxy" stopProcessing="true">
<match url="^external/(.*)" />
<action type="Rewrite" url="http://some-site/{R:1}" />
</rule>
("Enable Proxy" is checked in the ARR Server Proxy settings at the server level).
The above rewrite rule works fine in a very simple test app with a web.config containing the section, it works fine in a web forms app, but if I put the same rule into an MVC3 app (on the same machine, so identical config for IIS higher up) it never has any effect; the request flows through.
if it's just a rewrite (and not a reverse proxy) it works OK, e.g.,
<rule name="rewrite to internal" stopProcessing="true">
<match url="^internal/(.*)" />
<action type="Rewrite" url="different-internal/{R:1}" />
</rule>
...is fine.
I can get the reverse proxy rule to work if I add
routes.IgnoreRoute("external/{*pathInfo}");
in the Global.asax.cs class, so that my request for external/* doesn't hit the default controller, but I don't understand why. I think the URL rewrite module kicks in way before the Routing (see http://learn.iis.net/page.aspx/496/iis-url-rewriting-and-aspnet-routing/) so I'd expect there not to be a conflict between them.
Is the Routing module adding in "virtual" rewrite rules to the URL rewrite module, that are overriding my declared rewrite rules?
I had this exact same issue and it took me the entire day to find the solution.
Find: the ServoceModel tag in your Web.config file and add the serviceHostingEnvironment code as seen below:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
This will allow routes to be passed through to IIS to handle it.
One more tip, I recommend anyone having routing issues with their MVC projects to install Remote Debugger via NuGet. This will tell you what routes are being activated when.

Forwarding requests for relocated static content

I have a bunch of static content on a site that has always lived in the root directory like http://mysite.com/smiley.gif. I want to move it all to a subdirectory http://mysite.com/images/smiley.gif.
The problem is that years of content points to the first URL. What's the best way to forward those requests to the new locations?
We've recently done exactly this. We ended up using rewrite rules though IIS because they are applied very early in the pipeline and so was the smallest performance impact. Take a look at the UrlRewrite module for more info.
Just got our rules open, after installing the rewrite module in IIS, you add the following to your root Web.config. This rule would rewrite all *.gif requests, you might have to tailor it a bit.
<system.webserver>
<rewrite>
<rules>
<clear />
<rule name="gif" stopProcessing="true">
<match url="^(.*).gif" />
<action type="Rewrite" url="/images/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webserver>

redirect all requests to www.example.com to example.com in config without access to IIS

I'm currently planning to deploy a site with a third party hosting provider. I will only have access to the server via ftp and a tool similar to cpanel called WebsitePanel.
No access to IIS set up or configs.
Is there anyway to redirect http://www.example.com to http://example.com?
Place this in your web.config using your values for domain.com. This leverages the URL rewrite rules of the web.config and IIS 7.
<system.webServer> / <rewrite> / <rules>
<rule name="Remove WWW prefix" >
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.domain\.com" />
</conditions>
<action type="Redirect" url="http://domain.com/{R:1}"
redirectType="Permanent" />
</rule>
Typically, the "tool similar to cpanel" should give you this option.
Failing that, you should be able to:
a) set a custom 404 page pointing, to, say, myredirector.asp [or whatever server-side script you wish to use]
b) in myredirector.asp [or whatever] , do a server-side redirect as appropriate.
Not as clean as a straight IIS redirect, but it works pretty good.
I'd suggest you do this through the domain's DNS configuration, rather than through your application. It's much simpler and doesn't rely on application code to work (so if you deploy a whole new application, you don't have to remember to add any config entries or similar).
Same thing can be done to add the prefix www also. A blog post for the same at following URL:
http://karmic-development.blogspot.in/2013/10/add-prefix-www-automatically-in-url-in.html

Resources