Why doesn't url rewrite work? - asp.net

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.

Related

Httphandler for ASP Classic?

Is there any way in ASP Classic to catch HTTP request before it comes to that page? Like in ASP.Net we can catch request at Application_BeginRequest.
Actually what I'm trying to do is to redirect requests to some other pages if asking files from a certain directory. My application is hosted on IIS6. And I suppose IIS6 by default doesn't support for url rewrite. Directory browsing is already disabled.
It would be nice if it could be done just putting a web.config in that certain directory to redirect to other pages.
e.g.
Original Requests:
Https://stackoverflow.com/NoReadWriteFolder/file1.asp
Https://stackoverflow.com/NoReadWriteFolder/file2.html etc
and I'd like to redirect to
Https://stackoverflow.com/ReadOnlyFolder/someOtherFile1.asp
Https://stackoverflow.com/ReadOnlyFolder/someOtherFile2.html
I googled but so far no luck, any clue would highly be appreciated.
background:
My application runs for different companies (countries in actual fact) that use the same application.
I use include file called default.asp that runs as standard include for all the scripts I serve. I catch domain requests in URL, detect which company this and then redirect to a URL that I build that includes company code and a "URL prefix" which is added to the URL throughout the session.
To do this, I read URL using request.servervariables, set Session variables, work out my URL prefix and based on this, create revised URL which I then response.redirect.
I hope this may give you an idea for your situation.
For IIS6 I have used Ionic's Isapi Rewrite Filter
https://iirf.codeplex.com/
It supports URL re-writes and re-direction and should do exactly what you need.
On the examples and help pages there are examples for you to re-direct.

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.

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/

URL Redirects in ASP.NET

Ok, wierd problem I cant figure out. Hopefully someone where can. I have inherited a site that was developed with a very over-architected Content Management System. I am having problems now with the redirection functionality built into it.
This is on a dedicated Windows 2003 server running ASP.NET 3.5 sp 1. The redirects are stored in the database, and I have confirmed that the correct redirect is in place in the database. Finally, the file extension .html has been mapped in IIS to the ASP.NET ISAPI. And there is an HttpHandler created to redirect the .html requests. The default documents on the server, in order, are:
default.aspx
index.aspx
default.asp
index.asp
default.html
index.html
for this example, we have two redirects both pointing to the same content page. /example and /example.html
when requesting /example.html it correctly finds the appropriate redirect in the database and does its magic. Bueno. When requesting /example it gives a 404 page. Its not even the asp.net yellowish 404 generic error page. Its the standard vanilla IIS 404 response so it appears that asp.net is not intercepting these requests.
Let me know if any other information is requested and I will try to provide what I can. Thanks in advance for all the great recommendation I am sure will come from the community.
You should be able to map a wildcard extension to go through the ASP.Net ISAPI DLL is the solution.
Installing Wildcard Application Mappings (IIS 6.0) may also be useful.
Without rewriting the CMS, you can put a physical file in a new directory "/example". This will trigger ASP.NET to intercept the request, and hopefully load your page.
If you want to really hack it up you can change the IIS 404 page to be a .NET page in your application that can handle the original request and redirect to the page you really want.
Yes this is correct because /example is not pointing to any file, it is pointing to directory in the web server. Check that Default.aspx/ default.html or any other atleast one of them exists in your app.
If you are using ASP.Net MVC for REST then check your actions are properly written.

Why is the server ignoring rewrite on non ASPX files?

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

Resources