Why is the server ignoring rewrite on non ASPX files? - asp.net

Locally this works but on the server when i do something like /user/profile i get a 404 however if i do /user/profile.aspx it works. I also rewrite images from /s/example.png to /localLocation/example.png, this doesnt work. If i write /localLocation/example.png it does show up however it defeats the purpose of my rewriting. Also if i attach a .aspx to the end of any path i get a default page which shows the address of the rewrite. It is correct but only if i add .aspx. How do i get the server to apply the rewrite to all paths?

The problem is that IIS only invokes ASP .Net for certain extensions.
You need to configure IIS to use ASP .Net to handle every extension that you want to rewrite.
EDIT
If your web host runs IIS 7 in Integrated Mode (ask them), you need to add the HttpModule that handles the rewriting to the modules tag in system.webserver. See here

Related

Extensionless URL in ASP.NET 4.0 on IIS6

I have a ASP.NET 4.0 WebForms webapp running on a IIS6 webserver. I'm not allowed to make any changes to the webserver. I have a flex app embedded in this file called:
myapp.contoso.com/mysubapp/mysubapp.aspx
I'd like to only require the user to use the URL:
myapp.constoso.com/mysubapp
to reach the application and essentially hid the mysubapp.aspx permanently. I've been checking out URLRewrite and URLRewriting.net... It all looks to be a little much for this once instance in which I need it (if I need to add more rewrites in the future I'll use one of those frameworks). Is there a simple way to achieve this? I've checked out similar posts... it seems that I may need to write a simple one myself?
URL Rewriting using iis6 with no extensionless urls
How to deal with extensionless Url in ASP.Net and IIS6
The simplest thing I can think of is to rename your web page from mysubapp.aspx to default.aspx. This will allow users to request myapp.constoso.com/mysubapp and get your page. This should work if you have not removed default.aspx from the default document list in IIS.
Alternatively, you can add mysubapp.aspx to the list of default documents in IIS.

need to generate 404 for folders on IIS7.5 but not their contents

I would like to be able to hide the existence of folders in my DotNet 2.0 website which is being hosted via IIS 7.5 that contain files that must continue to be accessible. For example, I want a person's web browser to be able to retrieve the file domain.com/css/style.css (which is needed to render the page) but if the person tries to access the URLs domain.com/css/ or domain.com/css I want them to receive a 404 error. (not a 403 error)
By default IIS 7.5 will give a 403 error which would still allow an attacker to know about the existence of the folder. I realize that an attacker could easily discover the existence of the "css" folder by visiting the site in their browser and seeing that files are being pulled from the "css" folder. That said, this is a requirement of the project that I have to comply with.
In IIS6 I used to be able to set the hidden attribute for the folder which would give me the 404 behavior I wanted, but this is no longer the case in IIS7.5. (IIS 7.5 gives a 500 error if I try to do this) IIS 7.5 is another requirement of this project. Also, in IIS6 I want able to create a wildcard mapping that would cause all requests to route through DotNet which would then allow me to create a 404, but again, this does not appear to work on IIS7.5.
I have already tried creating handlers in the web.config ( node) which works on my staging system but not on my production system. Also, this method seems like overkill because I have to create a c# handler and have a handler entry in the web.config for each folder I want to hide. I'd like a simpler solution, but also, the solution just seems to not work on my production system.
Ideally, requests to the folder that do not include the trailing forward slash should NOT cause a 301 redirect but should also cause a 404.
You could always put a handler/ASPX page as the default page for directories, and have that handler return a 404 error code.

Url rewriting with asp.net. is there a configuration needed?

I'm trying to enable rewrited urls in my project.
it's very good described in this post: urlrewriting by scottgu
It works very well when im running it on localhost, but as soon as i upload it to my host (.net 3.5), it doesn't work! i always get redirected to a 404 page!
Is there a configuration needed to enable this?
as scottgu says no, but i don't find out why it's not working...
thanks
// UPDATE 2.09.2010
Is there actually a way to enable routing or rewriting without having iis7 or the ability to install a modul like ISAPI Rewrite on the server?
Looks like i got a bad asp.net host...
In your localhost environment you are probably running the website on your ASP.NET Development server. That server is set up to capture all request (* . *) and run them through the ASP.NET pipeline.
II6 on the other hand, is configured to only send some requests ( ie *.aspx, *.asmx, *.ashx) through the ASP.NET pipeline. So if you are trying to catch a request for an url like "/my/fine/url" that will never be passed to the ASP.NET handler, and thus not rewritten.
You can change this configuration in the Application configuration for the website:
Open IIS Manager and right-click on the website, choose Properties
On the tab "Home Directory", click "Configuration..." button.
Click "Insert..." button to insert a Wildcard application map.
In "Executable:" insert path to aspnet_isapi.dll, in my case C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll (note: this path may differ on you server).
Remember to uncheck "Verify that file Exists"
Click OK!
And so! All your requests should now be directed to the ASP.NET handler and hence caught in your URL rewriter, regardless of extension.
But I must admit that I'm a bit unsure as to how this will affect performance on you site, routing all requests for static files, css, images etc through the ASP.NET handler. Maybe someone else out there has something to say about that.
/Dennis :-)
There are two ways to get the extensionless routes in IIS6:
a) ISAPI rewrite or other ISAPI url rewriter
b) Use a wildcard mapping to aspnet_isapi.dll
See this blog post for detailed instructions.
Here is example how to use new System.Web.Routing within ASP.NET WebForms.
http://deepumi.wordpress.com/2010/02/27/url-routing-in-asp-net-web-forms/

overwriting a web.config root file to enable customerrors tag

I have a situation where I want to catch 404 errors fired by HTML pages (not just aspx pages) but I only have access to the web.config of the root folder of my website, and all sub directories (note, i don't have access to the actual IIS server and I cannot create applications or change settings)
So I did try the web.config customerrors on a subdirectory, and they do work, for ASPX pages only, not HTML pages, does anyone know why?
Note that the two answers above are correct for the usual case. However, IIS 6.0 and below can be configured to process HTML pages or anything else through ASP.NET. Also, IIS 7 has changed things radically - in effect, the ASP.NET pipeline is the IIS pipeline now, so that any piece of content is processed through any HttpModules.
Thus, in IIS 7 and above, anything you can configure for ASPX pages, you can configure for HTML pages.
You could have a look at the new routing capabilities for ASP.NET: http://msdn.microsoft.com/en-us/library/cc668201.aspx.
HTML pages are not parsed by IIS therefore are not affected by web.config settings. I am not aware of any way around this without configuring the settings in IIS.
To be a bit more specific than what Jeremy said, IIS maps different file extensions to different executables. By default it will be configured to let the .NET runtime handle .aspx files (in which case your web.config will be loaded & used), but it will serve the .html pages directly itself (& therefore fall back on its own 404 error handling).
Annoying, but I don't think there's much you can do beyond either having control of IIS, or by making your flat html pages into aspx pages (even though they contain no actual server-side content), to trick IIS into letting .NET handle them.

Why doesn't url rewrite work?

In asp.net 3.5, I'm rewriting the url
http://www.abc.com/archive/1108/harpersdecember
to the following
http://www.abc.com/article.aspx?docId=78
I'm using this code to do it:
Context.RewritePath("/article.aspx?docId=78");
It works fine locally but when I upload to the remote web server, I get a 404 when trying to reference the above page. Any suggestions why it works locally but not remotely?
You may need to create a wildcard mapping in IIS on the remote server so that all requests are processed by ASP.Net. If you do not do this any URLs without .ASPX on the end will not run through your URL rewriting code.
There is a good explanation of this (and other reasons you might use it) on Scott Guthrie's blog.
Not "may" - you definitely need to create a wildcard mapping. Visual Studio uses the cassini web server which essentially passes all requests through .net. IIS only forwards specific mapped requests (by default .aspx, .asmx, etc..) to .net - rewriting a URL in asp.net requires adding a new mapping to get the request to asp.net in the first place
Sounds to me like the production server does not have a default aspx page, ie: default.aspx. If it did, it would reroute the request to your handler.
Easy way to verify this, would be to create a directory and place a default.aspx file in it and try to request it using only the dir name, ie: server.com/newdir/
If that gives you a 404, then you know it for sure.

Resources