capture URL for custom 404

J-Will

[H]ard|Gawd
Joined
Jan 10, 2009
Messages
1,728
While trying to spare everyone from too much detail, my goal is to change a website from coldfusion to asp.net. However, the site has been around for a number of years and people have bookmarked pages. Making this change will obviously change file extentions and thus URLs.

So the question is this: Is there a way to capture the URL a user inputs, and redirect the user to the new page based on that particular URL. I would assume a single 404 could be created with several if/ then or case statements, then the URL could be passed as a variable to determine the correct new page.

So if a user attempts to connect to myawesomepage.com/awesomenesstest.cfm, it redirects them to a 404 telling them to update bookmarks and what not, then automagically redirects them to myawesomepage.com/awesomenesstest.aspx because somewhere in a statement of somesort we said that myawesomepage.com/awesomenesstest.cfm is now located at myawesomepage.com/awesomenesstest.aspx
 
Last edited:
A custom ASPX page and a Web.Config file entry would suffice. Make sure you're looking for and passing along any querystring parameters when you do the redirection.

Web.Config block
Code:
...
<customErrors mode="On">
<error statusCode="404" redirect="Nice-FileNotFound-Page.aspx"/>
</customErrors>
...

More details
 
Last edited:
Thanks PTNL, but its a step more complicated than that.

There are several pages on the site, each with a newly developed page. If the user goes to page1.cfm we want to redirect them to page1.aspx. If they go to page2.cfm we want to redirect them to page2.aspx. I guess it would be a more dynamic 404 with maybe a db backend or an XML/ flat file of some sort.
 
Thanks PTNL, but its a step more complicated than that.

There are several pages on the site, each with a newly developed page. If the user goes to page1.cfm we want to redirect them to page1.aspx. If they go to page2.cfm we want to redirect them to page2.aspx. I guess it would be a more dynamic 404 with maybe a db backend or an XML/ flat file of some sort.
You could do it that way...

My approach used only one defined redirect page. Set the Web.Config to take 404 errors to the temporary redirect page. On the page load event, capture the previous page requested, do a string swap of the file extension, and a Response.Redirect to the new desired page. Done and done.
 
oh nice, I will have to look into that. Thanks for the idea, I think that will be good as long as we keep the same file structure... which at this point we do plan to use.
 
just guessing but if op is using asp.net he's more likely using iis and not apache so i don't think .htaccess redirects would help :)
 
For the 301 redirects, I believe the following will suffice for the OP:
- Add another error statusCode node for "301" and point it to the temporary redirect page (or a totally separate page).
- On the destination page defined in the Web.Config, add lines like such before the final Response.Redirect() line:
Code:
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.new-location.com");
Above snippet found here

What you actually do with the final handling of the "301" code is your call: Response.Redirect or Response.End come to mind. Think about what is more applicable and desirable for your case.

I will remind to parse your failed Url, and make sure you prevent any cyclical loops between a failed page lookup and the temporary redirect page looping back to a non-existant file.
 
Last edited:
Back
Top