Asp.Net web.config Rules ignoring sub applications - asp.net

So my environment is C# asp.net sitefinity webforms / mvc / webapi hybrid.
I have an application pool in IIS 7 sitting behind an Amazon elb.
I have a site foo.com in IIS. I have added an application to foo.com called bar.
So my other site can be accessed by foo.com/bar.
These two sites work fine (for the most part). But sometimes when I access the same url
that belongs to foo.com/bar/* I will get a 404 error, I hit enter on the browser address bar
and it shows again. The 404s are hard to replicate some times I get a page rendered, some times I get a 404.
The thing is the 404 page I get is from foo.com, not foo.com/bar.
So it looks like sometimes foo.com is trying to resolve the url instead of foo.com/bar resolving the url.
I noticed that https traffic coming into the elb is sent to my application as http, so maybe the elb does something funny some times but not others?
Anyway, to troubleshoot I want my application at foo.com to ignore all url requests to foo.com/bar, so if i still get 404s I at least want the 404 page from the sub application (foo.com/bar).
So I am thinking of adding this rule to foo.com web.config
<rule name="Ignore bar" stopProcessing="true">
<match url="^.*foo.com/bar/.*" />
<action type="None" />
</rule>
But I'm unsure what the action does.
What does the none do? My intention is to have foo.com ignore the url and have foo.com/bar get the url if the regex is matched.
foo.com/bar is in test mode but foo.com is a live site, so I don't want to break anything.
btw I don't get 404s to foo.com/bar/* on my dev or staging environments (where the IIS applications are setup in teh same format as live).
The main difference seems be be the Amazon environment, but I could be wrong.
So will my rule work? Or is there a better way?
Edit: I don't know if this is related, but on my amazon instances sitefinty shows I have some HTTP PUT errors (when the elb I guess is trying to PUT something to my sub application (foo.com/bar), the error is a timeout).
Edit 2: When trying a Get call to a WebApi method (to foo.com/bar) on https I always get a 404 from foo.com. But the same url on http and I get the expected data, this seems to be consistently reproducible.

Try doing the following. On the webconfig of the child application. (foo.com/bar.)
Clear the rules and then define what you need:
<rules>
<clear />
<!-- Define your subapplication rules if needed -->
</rules>
That statement clear all inherited rules from parent application.
You can algo use:
<location path="." inheritInChildApplications="false">
All the lines wrapped between tags on the parent application would not be
inherited to the child application

I solved this.
The problem was above the amazon ELB we have ANOTHER akamai load balancer.
Akamai was caching certain urls and sending them to the wrong server so we got a 404 because the sub application wasn't on the server that Akamai was sending the request too.
Cleared the Akamai cache and now all the requests are working / hitting the correct server and application.
I didn't include this information in my question because I didn't know the live environment was setup this way.

Related

IIS localhost is redirecting to the live server instead of showing local aspx files

I need to redesign a site front-end to make it responsive, the site is based on C# and ASPX. I am familiar with working PHP local development environment by using WAMP so for this case I installed visualstudio web express because of its IIS server features for testing local development.
The client sent the folder from its ftp for me to work on so I have everything ready but the problem which I am facing is that when I try to right-click on the website folder from the solution explorer to view in the browser the localhost is redirecting to the actual live site which is running on the server instead of taking me to the localhost with local files so that I can have a view of my modification and changes.
I am unable to figuring out the problem may be there is something I need to change or replace in the webconfig file. I am working in asp environment for the first time your help and guidance in this regard will be very appreciated.
Many thanks.
I had this same problem just now as well as two or three times before and haven't pinned down a clear cut resolution, though I've tried removing redirects, checking the Global.asax, commenting out default document files in the Web.config, etc. I'm mainly working in Chrome on Windows 7, and after I cleared Chrome's cache localhost started working for me again. I don't know if the answer is as simple as that or if the solution comes from some combination of things. All I know is nothing else seemed to work, then clearing the browser cache did.
PROBLEM
I was facing this problem because I wrote this rule inside webconfig file.
<rules>
<rule name="CanonicalHostNameRule1">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.abc\.com$" negate="true" />
</conditions>
<action type="Redirect" url="www.abc.com/{R:1}" />
</rule>
</rules>
And because of this when I debug the solution the localhost is replaced with www.abc.com
SOLUTION
Just remove this rule if written
WHY USE RULE
These rules are used when you want to customize the url ,you can read further about Url Rewrite here
Redirecting to the live site from visual studio or localhost can be caused by a faulty url rewrite rule in the site web config. Even if the rule is deleted, the browser may cache the rule if it is a permanent redirect, and therefore clearing the browser cache and fixing the faulty rule can be a viable solution.
See article
https://theludditedeveloper.wordpress.com/2016/01/06/iis-url-rewrite-gotcha-2/
For me it was because the web.config had a rewrite rule added in it for SSL purpose. However, once I removed it, it still kept me redirecting to the live site. I wasted couple of hours searching the code without anything making sense. And then it turned out that the cache was the issue as the browser was still doing the redirection. So, I cleared the cache and it was solved.

IIS / ASP.NET: One URL returning 403 on one server despite other URLs in site working fine

I have taken over the maintenance of a production website at a new job that is written in ASP.NET 4 Webforms and that runs on IIS 6 on Windows Server 2003. I am not familiar with Webforms nor managing IIS...so I am kind of working things out as I go here.
I have done several deployments to our production server which have worked fine, but am now setting up a test environment that is identical, just a different IP address/domain, so we can properly test changes first.
I have a problem where on this test site any URL that does NOT end with a reference to a file (always .aspx on this site) will return a 403 error on this server. For example http://users.test.oursite.com/admin will always return a 403 error when logged into the site. It should be redirecting to http://users.test.oursite.com/admin/organisation.aspx. Having an MVC background I am not even sure how this happens...but it does in production.
Browsing links within the site is fine as they always reference an .aspx file. Manually typing URLS that reference an .aspx file is fine, just not when the URL does not contain a file. This is not a problem on the production server.
As I said, I am not familiar with WebForms or managing IIS itself...so I have kind of run out of places to look.
Is there anything that comes to mind that I should be looking at that could be causing this problem?
In WebForms typically there is no routing involved.
You need to either provide a full path on the URL ending with .aspx OR
setup default documents for your website. (Index.aspx, Default.aspx etc.)
http://www.iis.net/configreference/system.webserver/defaultdocument
OR
<configuration>
<system.webServer>
<defaultDocument enabled="true">
<files>
<add value="home.html" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
Default documents can be setup in IIS Properties or via web.config.
Otherwise you need to provide the full path.
And because you haven't setup the DirectoryBrowsing (in IIS), you get a 403 Error when you try to access the Directory.
Enable the Directory Browsing option in IIS (not recommended) if you want this error to go away.

URL Rewrite error with Web Applications

I am running an ASP.NET 4.0 website using IIS7 on Windows Server 2008 R2.
The site has a number of Web Applications under the main domain (~/site1, ~/site2, ~/site3, etc...). Each Web Application is an instance of the same website source folder (D:\Websites\MySite), so you can access the same website at any of the following URLs:
www.mydomain.com
www.mydomain.com/site1
www.mydomain.com/site2
www.mydomain.com/site3
The same website is returned each time, but with slightly different content depending on which specific URL is called.
Everything has been working fine for months until I was asked to add some SEO optimizations using URL Rewrite. Every URL Rewrite rule I add produces the following error:
HTTP Error 500.52 - URL Rewrite Module Error. The page cannot be displayed because an internal server error has occurred.
Config Error: Cannot add duplicate collection entry of type 'rule' with unique key attribute 'name' set to 'AddTrailingSlashRule1'
I have read elswhere that surrounding the <system.webServer> configSection in the web.config file with:
<location path="." inheritInChildApplications="false">
will fix this problem.
However, while it stops the error message from appearing, it also stops the URL Rewrite rules from working in any of the Web Applications (~/site1, ~/site2, ~/site3).
How can I get the URL Rewrite module to work with Web Applications?
Thanks for any help you can offer.
Add a clear to prevent the duplicate errors on subsites.
<rules>
<clear />
<rule name="Redirect rule for /maps">
....
</rule>
</rules>

httpModule for 404

I've created an httpModule to handle URL remappings, and it works great on my test system. A request for www.mydomain.com/Some_Fancy_URL gets rewritten to www.mydomain.com/some.aspx?fancy=23 and so on.
When I deploy to the actual web site, I'm getting the default IIS 404 page though.
After doing some research online, it would seem that I need to setup "Wildcard Mapping" in IIS 6 to get the request past IIS and in to my httpModule. The problem is that the site is hosted on a shared server, so it may not be possible to get the ISP to make that change.
My question is, can't I use an httpHandler to tell IIS how I want these requests handled? For example:
<httpHandlers>
<add path="*.aspx" verb="GET,POST" type="System.Web.UI.PageHandlerFactory" validate="false"/>
</httpHandlers>
It would seem like adding this to my Web.Config should tell IIS to stop validating the existence of .aspx files, and just pass the request along for me to process. It doesn't work though.
Any suggestions?
The problem with IIS 6 and ASP.NET is that they're aren't integrated. IIS needs to be told about ASP.NET via script mappings (.aspx, .asmx, wildcard and so on).
None of your web.config configuration settings will influence IIS because web.config is there to configure ASP.NET's behaviour, not IIS. IIS has no knowledge of web.config.
Unless you can hand off a request to the ASP.NET pipeline (via a script map) nothing will happen and all your web.config settings will be ignored.
With IIS 7 the story is quite different. In IIS7, ASP.NET and IIS are closely integrated and share the same pipeline thus permitting you to achieve the result you're looking for.
The alternative may be to find out if your hoster runs a URL rewriter such as ISAPI_Rewrite on their servers. That way you could rewrite urls without having to map a wildcard scriptmap to IIS6.
Through some trial and error, along with more web searches, I found a solution. It essentially parallels Kev's answer.
IIS won't treat a request as .NET unless it has a known file extension (.aspx, .ascx, etc.). When I send along something like www.mydomain.com/anything it looks for a file or folder named "anything", and when it doesn't find one, it just drops off to the default IIS 404 handler.
That's where I took over. I changed IIS 6 to forward 404 problems to /404.aspx. I then created that page with a generic "Your file wasn't found" message in the same style as my web site.
Here's the good part: Now that IIS is sending 404's to a .NET page, the custom httpModule I created is getting fired. The request is for 404.aspx, but IIS is nice enough to also append the original URL as well. You get something like:
www.mydomain.com/404.aspx?404;http://www.mydomain.com/anything
This allows me to parse the request in the httpModule, and rewrite as needed!

web.config urlmapping

I have a comment form on my website on contact.aspx. I want to be able to put up a redirect from /comment (no extension) to point to contact.aspx.
I set up the following url mapping in my web.config and when I test locally it works fine. When I post it to production, the redirect doesn't happen and I get the IIS 404 error.
<system.web>
<urlMappings enabled="true">
<add
url="~/comment"
mappedUrl="~/contact.aspx"/>
</urlMappings>
I'm assuming this is because IIS isn't serving up the request to the asp.net engine and I'm using a shared hosting environment (discountasp.net) so I don't have direct control over IIS to configure it there. I can always put in the subfolder and a default.aspx that will redirect for me, but I thought I would inquire about this route first.
What does the collective think?
Ask your web host if they can configure your virtual directory to serve all requests through ASP.Net.
Many hosts (mine included) are willing to do this. (Mention ASP.Net MVC, which requires this)

Resources