URL Rewrite error with Web Applications - iis-7

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>

Related

IIS ARR overrides application custom 404

I have several web servers and the proxy based on IIS ARR to balance the load between the web servers.
If user goes to not existing page web server returns custom "notfound" page with 404 status code (works correct if I do not use proxy) Unfortunately it has been overridden by the ARR and I see IIS standard 404 screen instead of returned from web server (as on picture http://i.snag.gy/v1xvY.jpg).
Does anyone know how this the overriding can be avoided to allow users see the custom 404 page?
Thanks in advance
Add
<httpErrors existingResponse="PassThrough"/>
to your web.config file. It should look something like this:
<configuration>
<system.webServer>
<httpErrors existingResponse="PassThrough"/>
</system.webServer>
</configuration>
If you're developing an ASP.NET application, put it in the application's web.config file, not the proxy's.

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.

Asp.Net web.config Rules ignoring sub applications

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.

Azure Website 301 Redirect - Where Do I Put It?

I want to direct some of my other domains to my primary domain which is hosted at a Windows Azure website.
(For the sake of those that find working with CNAME's and DNS a little "foggy" (like I did) I'm going to layout the details.)
I have the domain www.myDomain.example correctly resolving.
I now want to point www.myOtherDomain.example to www.myDomain.example
At my registrar I have created a CNAME to point
www.myOtherDomain.example to myInternalAzureSite.azurewebsite.net
and then successfully configured it in the in the Azure Website domain manager tool.
Now, when I enter www.myOtherDomain.example into a browser I get the proper web page at www.myDomain.example, however, the address in the browser is still www.myOtherDomain.example not www.myDomain.example as desired.
I understand the two most desirable ways to accomplish this are either:
Forward myOtherDomain.example (which costs $ at some registrars)
Do a 301 permanent redirect
If I have all that correct, I have found many suggestions of HOW to do the 301 redirect, however, I can't seem to figure out WHERE to actually put the redirect?
Windows Azure Websites run IIS. You can use URL rewriting to create rules to rewrite one URL to another.
Instructions:
Create a website in Windows Azure.
In the Scale section, select a web site mode of Shared or Standard and save changes.
In the Configure section, on the domain names group, add the old domain name (or names) and the new domain name and save changes.
In your domain registrar or DNS provider for the old domain and the new domain, change the DNS records to point to the new Windows Azure Website. Use a "CNAME (Alias)" record and point it to the website's domain on Windows Azure, like this: "mywebsite.azurewebsites.net."
Upload your new website's contents to Windows Azure.
In the root of the new website, create a file named Web.config with a contents like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect old-domain to new-domain" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.old-domain.example$" />
</conditions>
<action type="Redirect" url="http://www.new-domain.example/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Verify that any request to http://www.old-domain.example/path?query will receive a "301 Moved Permanently" response with a Location header for http://www.new-domain.example/path?query.
For documentation refer to Using the URL Rewrite Module.
For examples refer to Redirect to new domain after rebranding with IIS URL Rewrite Module and IIS URL Rewrite – Redirect multiple domain names to one.
There's no need to upload web.config file as there is one that you can access through the Azure interface.
Open the settings pane for your App Service and click App Service Editor (Preview) in the Development Tools section towards the bottom of the left hand menu.
Click Go to open the Editor in a new tab. You will see the web.config file on the left. Click it to edit in the main pane.
One word of warning - this editor autosaves as you type! I'm sure we'd all do this anyway but I'd recommend preparing your code in an editor and pasting it in.
I was able to add a section without having to manually restart the App Service.
You can also do the redirect by placing this code in your web.config file under the configuration node:
<configuration>
<location path="oldpage1.php">
<system.webServer>
<httpRedirect enabled="true" destination="http://example.com/newpage1" httpResponseStatus="Permanent" />
</system.webServer>
</location>
<location path="oldpage2.php">
<system.webServer>
<httpRedirect enabled="true" destination="http://example.com/newpage2" httpResponseStatus="Permanent" />
</system.webServer>
</location>
</configuration>
I've taken a simpler approach and just added the extra required domains to the Azure App Service via the Custom Domains menu - per the screenshot below. No need to touch the main websites web.config file at all. No need for any explicit 301 redirect. In Custom Domains page, just assign the domains, add the SSL bindings by uploading your HTTPS certificates - hit the site!

IIS Wildcard Mapping not working for ASP.NET

I've set up wildcard mapping on IIS 6, by adding "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll", and ensured "Verify that file exists" is not checked :
on the "websites" directory in IIS
on the website
However, after a iisreset, when I go to http://myserver/something.gif, I still get IIS 404 error, not asp.net one.
Is there something I missed ?
Precisions:
this is not for using ASP.NET MVC
i'd rather not use iis 404 custom error pages, as I have a httpmodule for logging errors (this is a low traffic internal site, so wildcard mapping performance penalty is not a problem ;))
You need to add an HTTP Handler in your web config for gif files:
<system.web>
<httpHandlers>
<add path="*.gif" verb="GET,HEAD" type="System.Web.StaticFileHandler" validate="true"/>
</httpHandlers>
</system.web>
That forces .Net to handle the file, then you'll get the .Net error.
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /test.gif
Version Information: Microsoft .NET Framework Version:2.0.50727.1433; ASP.NET Version:2.0.50727.1433
You can try use custom errors to do this.
Go into Custom Errors in you Website properties and set the 404 to point to a URL in your site. Like /404.aspx is that exists.
With aspnet_isapi, you want to use a HttpModule to handle your wildcards.
like http://urlrewriter.net/
You can't use wilcard mapping without using ASP.net Routing or URLrewriting or some url mapping mechanism.
If you want to do 404, you have to configure it in web.config -> Custom errors.
Then you can redirect to other pages if you want.
New in 3.5 SP1, you set the RedirectMode to "responseRewrite" to avoid a redirect to a custom error page and leave the URL in the browser untouched.
Other way to do it, will be catching the error in global.aspx, and redirecting. Please comment on the answer if you need further instructions.

Resources