473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Url Rewriting Using a Custom 404 ASPX Page

I have done a bit of research of Url Rewriting, but as yet have been unsuccessful at getting it to work well, and there are issues around what file types are supported and how much code you want to write.

While working on a similar, though partially unrelated, issue, I came across a possible alternate method. Before I pursue this method in full, I wanted to get opinions of others as to any unforseen issues I might encounter.

The method is this:

In IIS, replace the default 404 error page with a custom ASPX page. Make sure you don't try to process any errors in the web.config.

When a 404 is hit, the custom ASPX page is called. Although to the user, the URL they typed in is still visible, the ASPX page is now loaded, and the QueryString contains the original url typed in.

So if I type in:

http://localhost/some_path

I get redirected to my custom ASPX page with a QueryString containing:

404;http://localhost:80/some_path

This query string is encoded--so it must be decoded first to be useful. Also note that it added the port ":80" to the host name.

In my custom ASPX page, I can now parse this url and do my Url Rewriting. If an unknown url is encountered, I pass to an error page.

To me this is much easier and more elegant than trying to do Url Rewriting in other ways.

So my questions are:
* Has anyone else tried this, or is currently using this?
* Are there unforseen issues I will encounter with this approach?
--
Greg Collins [Microsoft MVP]
Visit Brain Trove ( http://www.BrainTrove.com )
Oct 5 '06 #1
3 4705
I would advice against doing that way.
It's actually really easy to get Url Rewriting working.
The hint is to rewrite URL back to original in your page.

A lot of things like search engines and robots rely on correct 404 code
thrown to know that page does not exist. Otherwise it would create infinite
website with the same page given out for any bad URLs. Plus analysing your
logs you will never know that some image is missing or link is broken on
your site.
Url rewriting simplified.

Global.asax
protected void Application_BeginRequest(Object sender, EventArgs e)

{

string sTmp;

HttpContext ctx = HttpContext.Current;

string sPath = ctx.Request.Path.ToLower();

ctx.Items["OriginalPath"] = sPath;

ctx.RewritePath("~/Default.aspx", "", "" );

}

Page default.aspx

private void Page_Load(object sender, System.EventArgs e)

{

//no caching

Response.CacheControl = "no-cache";

Response.Expires = -1;

HttpContext ctx = Context;

//rewrite URL back.

string sOriginalUrl = (String)ctx.Items["OriginalPath"];

string sOriginalQueryString = (String)ctx.Items["OriginalQueryString"];

if( sOriginalQueryString.Length 0 )

sOriginalQueryString = sOriginalQueryString.Substring(1,
sOriginalQueryString.Length - 1);

ctx.RewritePath(sOriginalUrl, "", sOriginalQueryString);

}
George.
"Greg Collins [Microsoft MVP]" <gcollins_AT_msn_DOT_comwrote in message
news:up**************@TK2MSFTNGP06.phx.gbl...
I have done a bit of research of Url Rewriting, but as yet have been
unsuccessful at getting it to work well, and there are issues around what
file types are supported and how much code you want to write.

While working on a similar, though partially unrelated, issue, I came across
a possible alternate method. Before I pursue this method in full, I wanted
to get opinions of others as to any unforseen issues I might encounter.

The method is this:

In IIS, replace the default 404 error page with a custom ASPX page. Make
sure you don't try to process any errors in the web.config.

When a 404 is hit, the custom ASPX page is called. Although to the user, the
URL they typed in is still visible, the ASPX page is now loaded, and the
QueryString contains the original url typed in.

So if I type in:

http://localhost/some_path

I get redirected to my custom ASPX page with a QueryString containing:

404;http://localhost:80/some_path

This query string is encoded--so it must be decoded first to be useful. Also
note that it added the port ":80" to the host name.

In my custom ASPX page, I can now parse this url and do my Url Rewriting. If
an unknown url is encountered, I pass to an error page.

To me this is much easier and more elegant than trying to do Url Rewriting
in other ways.

So my questions are:
* Has anyone else tried this, or is currently using this?
* Are there unforseen issues I will encounter with this approach?
--
Greg Collins [Microsoft MVP]
Visit Brain Trove ( http://www.BrainTrove.com )

Oct 5 '06 #2
There's a couple of issues I have with traditional Url Rewriting:

1. I can never seem to get it to work. I can install other web apps that use it, and they work... but if I try to do it myself (using any of the examples I've seen online) it never works right. For example, your code--as I understand it, the url will be redirected to default.aspx no matter what page I enter in. But if I go to localhost/foo.aspx, then I just get a 404 error.

2. Without much more complex code and other adjustments (which I don't understand at this point), you cannot deal with rewriting non-ASP.NET pages and folders. So a path like: localhost/foo/bar (i.e. no filename) or localhost/foo.bar (non-ASP.NET file extension) won't work.

As per your other arguments:

I can agree that the server logs will no longer show a list of 404 errors to check up on. . . but I think that this can be resolved by logging them yourself to a database, since you'll have the original url available to you via the query string. This could also be used for broken links.

I tested with a missing image, and that seemed to work fine (in the browser).

As for search engines and robots. . . many sites take advantage of a custom 404 page so that they can have their site framing around the error message. So it seems like these issues should already have been resolved. The object here is to handle the rewrite where possible, and then if not, then drop to a default error page anyway.

I'm not convinced that this is the correct route to go, and I'd rather not go this route if I could actually get traditional Url Rewriting to work. . . but after several months of beating my head on this subject, this looks like a viable alternative. I can have any path (with or without a filename, whether or not ASP.NET handles the file extension) sent to an ASPX page where I can process the original URL and perform the rewriting or drop to an error page.

Although your arguments may be valid, I'm not yet convinced that they are bad enough to keep me from going this route.

Again, I'd much rather do traditional Url Rewriting. . . if I could ever get the thing to actually work! Maybe I've just got some wierd IIS problem. So far as I know, I've got a default install on a WinXP SP2 x64 machine. I can't seem to get it to work on my shared hosting server either. What drives me nuts the most is that, as I said earlier, I can install some web app that uses it (like community server or .text) and they seem to work just fine.
--
Greg Collins [Microsoft MVP]
Visit Brain Trove ( http://www.BrainTrove.com )
Oct 6 '06 #3
Hi George (or anyone else),

Please feel free to bring up points against my arguments or thoughts... I want to understand all the consequences of this choice if I do choose to go this route.

I have heard of others using this method... so if those who are using it want to make comments, I'd appreciate that too.

I want to know what I'd be getting into!

Thanks.

--
Greg Collins [Microsoft MVP]
Visit Brain Trove ( http://www.BrainTrove.com )
Oct 7 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jon Maz | last post by:
Hi All, I am experimenting with URL Rewriting using the techniques outlined by Scott Mitchell in his article "URL Rewriting in ASP.NET"...
2
by: Jon Maz | last post by:
Hi All, I've been looking into options for URL Rewriting in .net, and to be honest, I haven't seen anything that's easier than the old Classic Asp solution with an ISAPI filter redirecting to an...
3
by: Michael Appelmans | last post by:
I'm trying to use a rule based URL rewrite application which uses HttpApplication.RewritePath. I keep getting "rsource not found" error in application when running on shared web host although the...
2
by: R-D-C | last post by:
Can these two functions coexist? We have a web site where the querystrings are removed using URL Rewriting. Instead the page appears to be a html page with a long name (containing what would be...
0
by: Lee | last post by:
Hi all ;) Preamble -------- I'm using URL rewriting to enforce a frames policy (yeah, I know frames are 'bad' :) - i.e. if a request comes in for a page which should be nested within a...
0
by: Netveloper | last post by:
Hi, I'm playing around with url rewriting and I have come across a problem which I can't seem to get past. The general ide is to have a IHttpHandlerFactory class which checks the incoming...
9
by: Frankie | last post by:
I understand that with URL rewriting we can have a request come in for, say Page1.aspx, and rewrite it so that PageA.aspx gets served up to the user. My question (assuming the above is correct):...
0
by: Hamayun Khan | last post by:
Hi I m developing site for my client using asp.net 2.0. He asked me for URL rewriting. which I have done using Intelligencia.UrlRewriter.RewriterHttpModule rewriting module. I write the...
3
by: IanW | last post by:
Hi, I'd just like some advice on the best way to throw a 404 when rewriting urls. The scenario is as follows: My url is /products/redwidgets This redirects fine to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.