Article 70Y6Z CodeSOD: Forward Not Found

CodeSOD: Forward Not Found

by
Remy Porter
from The Daily WTF on (#70Y6Z)

Anthony found this solution to handling 404 errors which um... probably shouldn't have been found.

function show_404($page = '') { $uri = $_SERVER['REQUEST_URI']; error_log("Caught 404: $uri"); $redirect_url = ""; switch($uri){ case "/SOMEURL": $redirect_url="http://www.SOMEWEBSITE.com/SOMEURL"; break; case "/SOMEOTHERURL": $redirect_url="http://www.SOMEWEBSITE.com/SOMEOTHERURL"; break; case "/YETANOTHERURL": $redirect_url="http://www.SOMEWEBSITE.com/YETANOTHERURL"; break; // ... THERE ARE 300 of these ... case "/MOREURLS": $redirect_url="http://www.SOMEWEBSITE.com/MOREURLS"; break; case "/EVENMOREURLS": $redirect_url="http://www.SOMEWEBSITE.com/EVENMOREURLS"; break; } if ($redirect_url){ Header( "HTTP/1.1 301 Moved Permanently" ); Header( "Location: $redirect_url" ); } else { parent::show_404($page); }}

Upon a 404 error, this code checks a switch statement. If the path portion (the REQUEST_URI) is in our switch, we redirect to a similar path on a different domain (I am only assuming it's a different domain; if it's the same domain, this is an entirely different class of WTF).

On the other hand, if we don't have an entry in our switch, we just show a 404 error.

I can see how this happened: someone migrated part of their site to a new domain, but didn't want to fix all the links (and can't fix all of people's bookmarks), so they wanted to automatically redirect the appropriate URLs. But here's the thing: this isn't the way to do it. In this particular case, the developers were using Code Igniter, a PHP framework for web apps, which has a routing engine; it would have been trivial to simply direct all the forwarded routes to a controller that just handles the redirects.

Or one of the many, many other options for redirecting users browsers that don't involve transforming a 404 into a 301 with a gigantic switch statement.

buildmaster-icon.png [Advertisement] Utilize BuildMaster to release your software with confidence, at the pace your business demands. Download today!
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments