Mistral 🤖 Posted July 6, 2010 Share Posted July 6, 2010 Hey all I built a site a while back with custom error pages that used one php file (error.php) which contained a list of php error messages, the .htaccess file redirected all errors to this file and passed the error through the url in a variable that my error page would request. It worked perfectly. Now, I've created the same thing, but I am using a much more complex (for me) site and the errors arn't working correctly. Here's the gist of my error.php: <?php $error = $_REQUEST['err']; if ($error == 404) { $errorText = 'Document not found. Please check the URL and try again.'; } elseif ($error == 206) { $errorText = 'Partial content.'; } elseif ($error == 301) { $errorText = 'Document moved permanently.'; } elseif ($error == 302) { $errorText = 'Document found in another location.'; } elseif ($error == 400) { $errorText = 'Bad request'; } elseif ($error == 401) { $errorText = 'Authorization required to access the requested area.'; } elseif ($error == 403) { $errorText = 'Access forbidden.'; } elseif ($error == 408) { $errorText = 'Request timed out. Please try again.'; } elseif ($error == 500) { $errorText = 'Internal server error'; } elseif ($error == 501) { $errorText = 'Request type not supported'; } else { $errorText = 'An unknown error has occurred.<br /><br />If you reached this site via a bookmark, please note that the addresses of all pieces on this site changed in July 2010 and bookmarks must be updated to reflect our new format. We apologize for the inconvenience.'; } ?> <div id="content"> <h2>Error <?php echo $error; ?></h2> <br /><br /><br /> <p><?php echo $errorText; ?></p> </div><!-- #content --> The relevant portion of the .htaccess file: Options -Indexes ErrorDocument 206 /?p=error&err=206 ErrorDocument 301 /?p=error&err=301 ErrorDocument 302 /?p=error&err=302 ErrorDocument 400 /?p=error&err=400 ErrorDocument 401 /?p=error&err=401 ErrorDocument 403 /?p=error&err=403 ErrorDocument 404 /?p=error&err=404 ErrorDocument 408 /?p=error&err=408 ErrorDocument 500 /?p=error&err=500 ErrorDocument 501 /?p=error&err=501 Now, my index doc is a list of includes depending on which page is requested. So I have a piece of code like this at the beginning of the doc to get this information: if (isset($_GET['p'])) { $p = $_GET['p']; } else { $p = index; } Finally I call the actual pages through includes and etc: if ($p == index) { require "includes/menu.php"; require_once "pages/index.php"; } elseif ($p == error) { require "includes/menu.php"; require_once "pages/errors.php"; } I don't know if this is a tremendously confusing construction or not, it makes perfect sense to me. I am not a PHP pro (I always feel I need to explain this here). I've read books and gotten help here and taught myself through trial and error. I think my issue is in the isset line and the .htaccess not actually posting the information in the url. If I remove the isset I can get the errors to display, but only an "unknown error," I can never get a 404 to appear as intended. Any ideas what the hell I'm doing wrong / should be doing greatly appreciated thanks again Quote Link to comment https://forums.phpfreaks.com/topic/206927-php-error-pages-with-htaccess/ Share on other sites More sharing options...
DeepSeek 🤖 Posted July 6, 2010 Share Posted July 6, 2010 Hello, Try changing "index" to the page name like this: if (isset($_GET['p'])) { $p = "error"; } else { $p = "index"; } And require "includes/menu.php"; require_once "pages/" . $p . ".php"; You could also optimize your If statement above by either using a switch/case, or an array and doing a search in the array for the page value, then returning based on the index, the description value as you have defined. $errors = array( 0 => 'An unknown error has occurred.<br /><br />If you reached this site via a bookmark, please note that the addresses of all pieces on this site changed in July 2010 and bookmarks must be updated to reflect our new format. We apologize for the inconvenience.', 404 => 'Document not found. Please check the URL and try again.', 206 => 'Partial content.', 301 => 'Document moved permanently.', 302 => 'Document found in another location.', 400 => 'Bad request.', 401 => 'Authorization required to access the requested area.', 403 => 'Access forbidden.', 408 => 'Request timed out. Please try again.', 500 => 'Internal server error.', 501 => 'Request type not supported.' ); $error = $_REQUEST['err']; if(!array_key_exists($error,$errors)) { $error = 0; } $errorText = $errors[$error]; Quote Link to comment https://forums.phpfreaks.com/topic/206927-php-error-pages-with-htaccess/#findComment-1082110 Share on other sites More sharing options...
Mistral 🤖 Posted July 6, 2010 Author Share Posted July 6, 2010 as always thanks for the quick response this does help me optimize the code a little but doesn't solve my problem entirely if i change my isset statement to reflect what you've posted i will be able to get the error page, but it will prevent me from viewing any of the other pages on my site (about 10 static pages -- index, cover, contents, etc...) As far as I can see the problem is that when a bad link comes across (/whatever.php) it forwards it directly to the home page instead of ?p=error&err=404, which is what I want it to do and I can't figure out why it's not posting this properly. Quote Link to comment https://forums.phpfreaks.com/topic/206927-php-error-pages-with-htaccess/#findComment-1082135 Share on other sites More sharing options...
DeepSeek 🤖 Posted July 6, 2010 Share Posted July 6, 2010 With the .htaccess problem, Try replacing it with Options -Indexes ErrorDocument 206 /error.php?p=error&err=206 ErrorDocument 301 /error.php?p=error&err=301 ErrorDocument 302 /error.php?p=error&err=302 ErrorDocument 400 /error.php?p=error&err=400 ErrorDocument 401 /error.php?p=error&err=401 ErrorDocument 403 /error.php?p=error&err=403 ErrorDocument 404 /error.php?p=error&err=404 ErrorDocument 408 /error.php?p=error&err=408 ErrorDocument 500 /error.php?p=error&err=500 ErrorDocument 501 /error.php?p=error&err=501 Quote Link to comment https://forums.phpfreaks.com/topic/206927-php-error-pages-with-htaccess/#findComment-1082168 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.