Website deployed on IIS Server displays a blank page - asp.net

The main problem is specified in the title; however, it is a little more nuanced than that.
I cannot give away the exact address, but the URL has the syntax: https://name.mywebsite.com.
If I go to that exact address, the page will load as intended. If I try to be more specific about where exactly I want to go on the page - let's say to: https://name.mywebsite.com/home - I get a blank page.
Curiously enough, after I go to https://name.mywebsite.com, all the other addresses work as well and either redirect me to that address or display the site that they are supposed to show. I figured out that some necessary Cache-Data and data for the local storage are not set if I don't call the site over the mentioned address.
All of that wouldn't be much of a problem, since only the first call of the website is affected. However, most users of the site will first call it with a QR-Code (which leads to a specific sub-page of the site), and it wouldn't be very nice if they are greeted with a blank page.
Also, it is a .NET Core App with Angular Frontend.
I can reproduce the error by clearing my cache and cookies. The necessary data is no longer set and all domains will display a white page - except https://name.mywebsite.com - again.
I think I understood the problem, but I cannot seem to come up with a solution. I tried some changes in the IIS Manager on the server, but to no avail. My main bet was to apply an inbound rule with the URL Rewrite function, but that did not work.
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect from first shop open" enabled="false" stopProcessing="true">
<match url=".*" />
<action type="Redirect" url="https://name.mywebsite.com/" appendQueryString="true" logRewrittenUrl="true" redirectType="Found" />
</rule>
</rules>
</rewrite>
</system.webServer>
I also tried that rule with all possible Redirect types. Those rules do alter the page, but not in the way I want it. Instead of the blank white page I get an error that the redirect failed (with the hint that it is probably due to a lack of cookies). That error applies to all pages, even https://name.mywebsite.com. So I have no way of accessing the website now.
I also tried to find any other configuration that might prevent other URLs from loading the data, but I did not find anything like that either (or I simply looked at the wrong places).
Edit: /home would be a route in Angular. Also, the only rewrite rule that exists and that I tried is the one shown in the picture (which I updated so that it fits better with the question). I made some changes to the question in general.

As it turns out, I simply had to make some changes to the rule I posted in the question. Changing the Redirect to a Rewrite that targets /index.html and adding some conditions (which should be adapted to your personal needs) did the trick and the website no longer shows a blank page.
The link from Lex Li in the comments from the questions angular.io/guide/deployment#fallback-configuration-examples and the link https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#accessing-url-parts-from-a-rewrite-rule helped me building the correct rule.
<rewrite>
<rules>
<rule name="Angular Routes" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{REQUEST_URI}" pattern=".*/(home|item.*)" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" logRewrittenUrl="false" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name=".*" />
</rewriteMaps>
</rewrite>

Related

Rewrite rule that works for different domains and subdomains

I am wondering if it is possible to set up a rewrite rule where you do not specify the domain, only the page accessed and the page to redirect to. My goal for this is to not have to have different versions of the same rule for different sites (i.e. dev.mysite.com, qa.mysite.com, etc), so one rule works for multiple subdomains. For example, I need to redirect someone going to dev.mysite.com/mexico to mx.dev.mysite.com/es/mexico, but that also has to work for QA. Additionally, production has a slightly different domain which would make this even more difficult. This is what I came up with - it definitely needs work:
<rule name="Test Mexico Redirect">
<match url="^mexico"/>
<action type="Redirect" url="http://mx.{R:2}/es/mexico" redirectType="Permanent"/>
</rule>
Unfortunately it just does not work; I get a blank page. Probably because {R:2} is looking for the domain in the rule and one isn't specified. I can't think of a way to do this, because not even the domain is the same across all environments.
It would be cool if you could use regex in the <action> element, then I would do something like this: ^(mx\.).*com/es/mexico.
It's possible using {HTTP_HOST} and back references like {C:#}. The IIS Rewrite module is extremely useful for setting this up. Here is a screenshot with some notes that explains how it's done:
By using {HTTP_HOST} and conditions I was able to make a rewrite rule that can apply to all environments. The actual XML result:
<rule name="Mexico Redirect" stopProcessing="true">
<match url="mexico" />
<conditions>
<add input="{HTTP_HOST}" pattern="^((?!mx))" />
<add input="{CACHE_URL}" pattern="^(.+)://" />
</conditions>
<action type="Redirect" url="{C:1}://mx.{HTTP_HOST}/es/search" redirectType="Temporary" />
</rule>
Also I may have been incorrect saying you won't need to recycle the app pool with a Temporary (307) redirect. I still needed to recycle it but only in production... no verdict on this yet.

ASPX site down page

I have to take a site down for maintenance, and need a site down page.
I have come across the app_offline.htm page, which seems to do what I want.
However, when I put it on one of the test sites it did not work properly and required a bit of extra work to get it working properly.
Given this, and the fact that I cannot test it on the live site, I am trying ot create a backup plan.
So I have the following web.config rule:
<rule name="Site Down Rule" stopProcessing="true">
<match url="(.*)"/>
<conditions/>
<action type="Redirect" url="app_offline.html" />
</rule>
Which should redirect to the custom page.
The problem is that it gives the following error: ERR_TOO_MANY_REDIRECTS, which I assume is because when it reaches the given page, it still tries ot redirect.
But I do not know how to solve this. I have seen this question:HTTP redirection issue in IIS, keep getting ERR_TOO_MANY_REDIRECTS on the browser
and the related question it references, but these do not provide a working solution.
Any ideas?
EDIT:
Is it possible to add an exclusion to this rule - which says do not redirect given page
Got it - I just needed to exclude the page that was being redirected to:
<rule name="Site Down Rule" stopProcessing="true">
<match url="(.*)"/>
<conditions>
<add input="{REQUEST_URI}" negate="true" pattern="app_offline.html" ignoreCase="true" />
</conditions>
<action type="Redirect" url="app_offline.html" />
</rule>

Web Config Rewrite/redirect

I am working on an existing project that I did not create.
In the web.config file, there are a number of redirects, setup in the following way:
<rewriteMaps>
<rewriteMap name="Redirects">
<add key="/findyourlocalbranch/" value="/find-your-local-branch/" />
</rewriteMap>
</rewriteMaps>
It seems to me that a rewrite rule is being used to do these redirects, but as I said, I did not write this and am not in a position to change this. I am not sure if this is right or wrong.
My problem is that I need to implement the following redirect:
<add key="/need-a-loan/.htm" value="/need-a-loan/" />
but this brings the whole site down - I suspect due to a syntax error in this line.
I suspect the error is the '.' after the '/'
My question is, what can I do about it - there is nothing on the internet that I can find about this.
Basically, I want to redirect this one, specific URL to the given URL???
I can't say whats going on with the map, as without the corresponding rule I'm blind.
But, this rewrite rule will redirect from /need-a-loan/.htm to /need-a-loan/.
<rewrite>
<rules>
<rule name="Need-A-Loan" stopProcessing="true">
<match url="need-a-loan/.htm" />
<action type="Redirect" url="need-a-loan/" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Note: doesn't use the map, so you'd remove that entry.
That will give you a 301. If you want a different type, you can adjust the rediectType. E.g. Temporary will give you a 307.

IIS URL Rewrite for Clean URLs

I am working on a site that uses IIS. I have it working to remove extensions but then I was informed that the client was everything removed after the trailing slash.
I first thought this might not be possible with URL Rewrite, but then again I don't know much about IIS.
Basically if anyone browses the site and clicks on an interior page the url should stay the same, the top level domain.
http://www.example.com instead of http://www.example.com/whatever
this is what I have so far to remove the extension.
<rule name="Remove PHP Extension">
<match url="(.*)\.php" />
<action type="Rewrite" url="{R:1}" />
</rule>
<rule name="Remove PHP Extension">
<match url=".*" negate="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{URL}" pattern="(.*)\.(.*)" negate="true" />
</conditions>
<action type="Rewrite" url="{R:0}.php" />
</rule>
Any help would be greatly appreciated
Thanks
Doing it via IIS may be difficult since each link would actually have to navigate the user to the page in the first instance. And then you're stuck, unless you then redirect to the homepage and then perform a rewrite based upon the referrer - which is all a bit nuts.
Instead, you could use an javascript/jquery approach which may be better. Something like:
$('a').on('click', function(ev){
ev.preventDefault();
$('body').load($(this).attr('href'));
})
This would take any anchor and override it ensuring th the content was loaded into the current page.
Admittedly, this is without any conditional checks for the href pointing to an page id etc. and also falls short when considering you may already have ajax calls in tht esite.
It also doesn't take into consideration forms submittals, but you could work this in.
Another problem with this is what would happen if someone navigated to a page directly.
A final approach would be to load the entire site into an Iframe. Nasty but apprpiate if that is the effect the client wants. You could have some javascript on each page that forced itself into an iframe if it wasn't already.
Whichever approach you take, your client can kiss goodbye to any search rankings. Which reminds me, don't forget to disallow all in your robots.txt

ASP.NET / IIS7 Url Rewrite maps not working

I've followed the instructions Learn IIS's webpage for adding static redirects with a rewrite map for my asp.net application.
The following is the config:
<rule name="Redirect rule1 for Information" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{Information:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" appendQueryString="true" />
</rule>
And
<rewriteMaps>
<rewriteMap name="Information">
<add key="/Information/CorporateSales.aspx"
value="/KB/Information/CorporateSales" />
<add key="/Information/ComputerRepair.aspx"
value="/KB/Information/ComputerRepair" />
</rewriteMap>
</rewriteMaps>
This was even originally created by the wizard in IIS's manager for using rewrite maps.
So the idea is that /Information/CorporateSales.aspx --> /KB/Information/CorporateSales with a 301 redirect (MOVED PERMANENTLY).
However I'm just getting the original aspx page (Which we're removing later) loading. I've even deleted the file incase it was defaulting to an existing resource, and with that i just get a plain 404 without the redirect.
Anyone have an idea?
Let me clarify something:
Rewrite module works, it's installed and running. My standard regex rules work nicely. But my rewrite map does not.
This article http://www.iis.net/learn/extensions/url-rewrite-module/using-rewrite-maps-in-url-rewrite-module and code below worked for me.
<rewrite>
<rules>
<rule name="Redirect rule1 for RedirectURLs">
<match url=".*" />
<conditions>
<add input="{RedirectURLs:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="RedirectURLs">
<add key="/privacy.php" value="/privacy" />
</rewriteMap>
</rewriteMaps>
</rewrite>
I was having a similar problem and found this question. It took me a little while, but I was able to figure out what the problem was.
My rewriteMap contained the urls "/Default2.aspx" and "/Dashboard.aspx".
When I would go to Default2.aspx, I would get a 404 rather than get redirected to Dashboard.aspx as expected.
The issue I found was that on my machine, the application was running in a subdirectory. The rewriteMap paths would only work if I used the full path (including the application folder), e.g., "/TestSite/Default2.aspx".
So I could have added duplicate entries in my rewriteMap to account for application directories on developer machines, but that seemed messy. I looked at the other rewrite rules in the application that did not have this issue and I noticed that they were using the {REQUEST_FILENAME} variable, rather than {REQUEST_URI}. So I switched the rule to use {REQUEST_FILENAME} and remove the first slash from the urls in my rewriteMap.
Do you have Url rewriting installed as part of IIS7/7.5? This is not installed by default. Also, make sure your app pool is set to integrated pipline mode, no classic.
Edit
From this:
http://learn.iis.net/page.aspx/469/using-rewrite-maps-in-url-rewrite-module/
This only thing I see that you're doing is adding the 'stopProcessing' attribute. Have you tried removing that?
Previously I had same problem as you described.
Could you update your code to
<match url="(.*)" />
and I hope you aware,
<add input="{Information:{REQUEST_URI}}" pattern="(.+)" />
this condition will capture full URL except the domain.
example on this url:
www.example.com/Information/CorporateSales.aspx
it will check matching condition of
Information/CorporateSales.aspx on rewriteMap
and for sure it wont be match with this url
www.example.com/old/Information/CorporateSales.aspx
Did you reset the app pool and the iis site ?
In some cases it can take up to 72 hours (iirc) to propagate throughout the world.

Resources