Reputation: 153
I have a database driven ASP.NET site, which uses a single APSX page to display all site pages. So all the URLs of the site are in the following format:
/main.aspx?page=Page+Title+One
/main.aspx?page=Another+Article+Title
Unfortunately Google has indexed many non-existent URLs, mostly cutting part of the query string. For example:
/main.aspx?page=Page+Title or /main.aspx?page=Page
instead of the correct one:
/main.aspx?page=Page+Title+One (this is indexed by Google as well of course).
What I'm trying to achieve is, if the query string page name doesn't have a matching entry in my DB, to force a 404 error server side, first for better user experience and second to tell Google that these pages don't exist.
Here is my code and is not working. I mean it just displays an empty page in IE 8, and error in the latest Firefox:
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.StatusCode = 404;
System.Web.HttpContext.Current.Response.Status = "404 Not Found";
System.Web.HttpContext.Current.Response.End();
I see the proper 404 code in my logs however I don't get redirected to my error page specified in my web.config:
<customErrors mode="On">
<error statusCode="404" redirect="404.aspx"/>
</customErrors>
What am I doing wrong?
Another thing that worries me a bit is if I successfully force 404 error on any query string entries that don't have a match in my db, wouldn't that affect the main.aspx page itself effectively affecting all my existing pages as well? Here is what I mean:
I 404 main.aspx?page=Non-Existing-Title, however my 404 affects main.aspx with any query strings including valid ones, for example main.aspx?page=Existing-Title.
The site runs on IIS6/Win 2003.
Thanks for your help!
John
Upvotes: 3
Views: 1517
Reputation: 49195
Why not build search engine friendly URLS - its quite easy with ASP.NET routing. Here's another quick-start for using routing with web-forms.
Because you have data-driven site, I would also suggest you to build a site-map of valid URLs on say daily basis and submit to search engines.
As far as custom errors goes, may be web server (IIS6) is overriding to display its own custom error pages - check this for managing custom error messages at IIS end.
EDIT:
Currently you see blank 404 page because you are not sending any error page content (your code sets the status code and then ends the response). You can of course use IIS side setting to have your custom error pages but if you want to show your aspx page then I would suggest that you do Server.Transfer
to your error page (404.aspx). In 404.aspx, you must set the status code to 404 but don't do Response.End
otherwise the error page content won't be served.
Lastly remember that browsers has a threshold for minimum content size in error pages, if content size is less then it will show its own error page. For IE, the size is 521 bytes for 404 error. See this blog post: http://perishablepress.com/press/2008/01/21/important-note-for-your-custom-error-pages/
Upvotes: 2
Reputation: 3523
It won't use the custom errors because there is a page there called main.aspx.
The best way to do this IMHO would be to create an HTTPModule and do your checking in there.
E.g. from http://forums.asp.net/t/762031.aspx/1
public class ErrorModule : IHttpModule
{
//Default implementation details left out…
private void FakeA404(Object source, EventArgs e)
{
// do your URL checking here ..
HttpContext.Current.Response.StatusCode = 404;
}
public void Init(HttpApplication context)
{
context.AuthenticateRequest += new EventHandler(this.FakeA404);
}
public void Dispose(){
}
}
However I do agree 100% with VinayC regarding the friendly web urls. You can set your routing up so that it will goto your current main.aspx?page=blah+blah2 - alternatively you can use an HTTPModule (early enough in the chain to effect things) to rewrite the path.
With your existing links I would issue a 301 perm redirect to the new url so google will get update nicely.
Another option would be to use a different delimiter as obv google doesnt like the +. I would use an underscore Page_Title_One
Upvotes: 0