IIS URL Rewrite for Dev environment and production environments - asp.net

I am trying to define the URL Rewrite rules for dev and for production. But I do have a lot of trouble getting them working.
The problem should be simple. The is a SPA with ASP .Net skeleton (used only for loading the site + Web API) + REACT JS.
The routes for Rest API are called with API (I need to add those in the below), but the problem that I have is regarding the client routes. Every time when I add something that's not a file I do get the 404 for js or css, or if I add the change is href (but I don't want) then I get problems in production environment.
Here is what I have so far:
<head>
<base href="/">
</head>
This is the rule for the dev. environment, that of course does not work right now.
<rule name="SPA Routes localhost" stopProcessing="true">
<match url="(.*)localhost\web.app(.*)" ignoreCase="true" negate="true" />
<action type="Rewrite" url="/web.app" />
</rule>
And this is the rule for the production environment:
<rule name="SPA Routes" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/" logRewrittenUrl="true" />
</rule>
I did try combination of those, as well. I do have a lot of trouble with debugging this. It seems to me that I need to brute force all the combinations.
Please help me with:
1) Debugging option for this. If you know any way to debug this problem, I will appreciate any insights.
2) Do you see anything that I am doing wrong?
Thanks a lot for the support!
// Note: I am using IIS 10

Related

IIS 7.5 with URL Rewrite Module Doubles QueryString Params on Postback

I'm using IIS 7.5 on Windows 7 RC. I use the IIS Url Rewrite module to rewrite URLs.
Everything seems to work fine, until I perform a postback by clicking a button. It then appends the querystring params to my rewritten URL, like this:
Rewritten URL, as it appears in the browser:
http://localhost/en/product/1239/Gary+Fisher+Hkek+Mountain+Bike
Without URL rewriting the URL is:
http://localhost/product.aspx?lang=en&id=1239&title=Gary+Fisher+Hkek+Mountain+Bike
When I click a button to perform a postback, the URL changes to this:
http://localhost/en/product/1239/Gary+Fisher+Hkek+Mountain+Bike?lang=en&id=1239&title=Gary+Fisher+Hkek+Mountain+Bike
And when the URL is rewritten, all querystring params are doubled - so when I want to get the current language by doing this:
Request.QueryString["lang"]
The value I get back is "en,en".
Is anyone else having those problems?
UPDATE: Rewrite rules from Web.Config
<rule name="RedirectProductPageUrls" stopProcessing="true">
<match url="^product\.aspx$" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_METHOD}" negate="true" pattern="^POST$" />
<add input="{QUERY_STRING}" pattern="^lang=([^=&]+)&id=([^=&]+)&title=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}/products/{C:2}/{C:3}" appendQueryString="false" redirectType="Permanent" />
</rule>
<rule name="RewriteProductPageUrls" stopProcessing="true">
<match url="^([^/]+)/product/([^/]+)/([^/]+)/?$" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="product.aspx?lang={R:1}&id={R:2}&title={R:3}" />
</rule>
Add the appendQueryString="false" attribute to the action element of the rewrite rule as well.
Hope this helps.
I was able to solve the issue by adding
Form.Action = Request.RawUrl;
to the Page_Load event. I was able to leave appendQueryString="TRUE" and so far it is working properly.
This is a security feature of the IIS Rewrite Module.
I personally prefer ISAPI Rewrite as it is much better, simpler to write rules, and has more features.
Have also found under moderate to high load (over 100 connections to a website) that the IIS Rewrite Module courses the application pool to crash and spawn and new process.

web-config url rewrite too many redirects issue

When I try to redirect all http requests to https with the following rules "too many redirects" occur...
when I turn the first rule's enabled to false, its ok but just form pages redirect.
thank you very much for your replies.
<rules>
<clear />
<rule name="Redirect to https" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{URL}" matchType="Pattern" pattern="\/Admin\/(.+).(aspx)" negate="true" />
<add input="{URL}" matchType="Pattern" pattern="\/modules\/(.+).(aspx)" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" redirectType="Permanent" />
</rule>
<rule name="CanonicalHostNameRule1">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^www\.xxxxx\.com$" negate="true" />
</conditions>
<action type="Redirect" url="https://www.xxxxx.com/{R:1}" />
</rule>
<rule name="km-old-rdr" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{URL}" pattern="\/kategori\/(.+).(aspx)" />
<add input="{URL}" pattern="\/kategori\/(.+).(aspx)" />
<add input="{URL}" pattern="\/urun\/(.+).(aspx)" />
</conditions>
<action type="Redirect" url="{C:1}" />
</rule>
<rule name="KM301" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{URL}" pattern="(.+).(axd)" negate="true" />
<add input="{DB:{R:0}301}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
<rule name="KMRewrite" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{URL}" pattern="(.+).(axd)" negate="true" />
<add input="{DB:{R:0}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:0}" />
</rule>
</rules>

Angular2 - page refresh 404ing when hosted in Azure

I am working on an Angular2 app. It uses "#angular/common": "2.0.0-rc.4" and "#angular/router": "3.0.0-beta.2".
My problem is that when I use the browser refresh on some of the pages I see an error saying...
"The resource you are looking for has been removed, had its name changed, or is temporarily unavailable."
This also happens if I hit the url directly.
An example url is...
https://tilecasev2.azurewebsites.net/profile/therichmond
However if you view pages via the homepage they work ok but only until refreshed (https://tilecasev2.azurewebsites.net).
I have the below in my index.html head...
<base href="/">
Why does this happen and how can I fix it?
HashLocationStrategy avoids the issue by including a # in all of your angular routes but doesn't really fix it.
To make angular routes without hashes work in azure the same way they do in your local development environment, you just need to configure IIS to rewrite all requests as root. This lets angular handle the routing.
To do this, add a Web.config file to your site's root folder with the following contents:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Main Rule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
As Gunter pointed out HashLocationStrategy needed to be setup.
I followed the steps in the Angular2 docs and it all works now...
https://angular.io/docs/ts/latest/api/common/index/HashLocationStrategy-class.html

url rewrite/redirect on iis7 redirect to mobile site

I have made all of my mobile folder/pages structure like the desktop version.
I need to make sure all the requests from mobile devices will be re-directed to a mobile domain.
what I would like to do is if a URL is opened from a mobile device like this: http://www.example.com/test/test.asp will be redirected to http://m.example.com/test/test.asp
this question might be asked before but I need to make sure it works.
Thanks very much
Take a look here:
http://forums.iis.net/t/1169853.aspx
This amends your web config to redirect -- not sure if it can be used on the page level, but it might be a good start.
I had the same problem and solve it.
<rule name="Mobile Redirect" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTP_USER_AGENT}" pattern="midp|mobil|phone" />
<add input="{HTTP_X-Device-User-Agent}" pattern="midp|mobil|phone" />
<add input="{HTTP_X-OperaMini-Phone-UA}" pattern="midp|mobil|phone" />
</conditions>
<action type="Redirect" url="http://m.yoursite.com/{R:1}" appendQueryString="true" />
</rule>

IIS Rewrite not working (but redirection does)

I was trying to play with URL re-writing using the Rewrite Module 2.0 but I had no luck getting it to work. What I'm trying to do is re-write all calls to web app at port 80 to other applications hosted in IIS (or maybe on different servers on the network). Using the GUI provided by IIS I created the following rule:
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="site1/(.*)" />
<action type="Rewrite" url="http://localhost:7001/{R:1}" />
</rule>
</rules>
</rewrite>
Quiet simple, but unfortunately it does not work. On the other hand, when I change the action type to Redirect, it works fine.
What could be the problem?
I ran into this same issue yesterday, and it took me a long time to figure out.
The key here is that you've got an http:// prefix in your rewrite action; that makes this a special case that needs to be handled by Application Request Routing. The first step is to make sure that the Application Request Routing module is installed. You can find the module at https://www.iis.net/downloads/microsoft/application-request-routing. Once that is installed, go to your IIS web server (a level up from your web site), and open the Application Request Routing Cache feature. From the actions on the right, choose Server.Proxy.Settings, and make sure that the "Enable Proxy" checkbox is checked. This allows the URL rewrite task to be re-routed to Application Request Routing, and your reverse proxy should work for external requests.
The idea came from this excellent blog post from 2009: http://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/
Stumbled across this old post when I was trying to solve the same issue.
SOLVED!
Using Rewrite URL feature in IIS Services Manager I created a friendly URL rule.
This worked ok and when I looked at the rule in the web.config file (www root) it showed 1 rule to redirect and 1 rule to rewrite.
I edited this to suit 1 match. Then I just duplicated this code editing the product ID for each. Example below:
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^product\.php$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^id_product=\b35\b" />
</conditions>
<action type="Redirect" url="990mm-bohemia-cast-iron-electric-radiator" appendQueryString="false" />
</rule>
The first rule looks for the string "product.php" in the URL and "id_product=35", it then redirects to "990mm-bohemia-cast-iron-electric-radiator" which currently does not exist. Then (see below)
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^\b990mm-bohemia-cast-iron-electric-radiator\b" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="product.php?id_product=35" />
</rule>
This rule rewrites the "product.php?id_product=35" bit to `990mm-bohemia-cast-iron-electric-radiator", creating the new location for the redirect.
Change the Rewrite URL to AbsolutePath instead putting http://...
it should be
It worked for me, but in my case, I have been rewrite to a fixed webpage.
I had 404 error with url rewrite. Maybe you can find it usfull. Please check it at https://stackoverflow.com/a/47988082/5784543

Resources