thebadbad Posted October 13, 2007 Share Posted October 13, 2007 I'm coding a website driven by one main document (index.php) outputting different include-files via different query strings. I have two levels of pages, each page outputted via a query string (e.g. main pages "blog", "map" and "about" and sub pages "blog_tag", "map_country" and "about_me"). I then use Apache's mod_rewrite to rewrite the ugly URLs (e.g. "index.php?page=blog" rewritten to /blog/ and "index.php?page=blog_tag" rewritten to /blog/tag/) thus making it easier and prettier to distinguish between main and sub pages. Below is my .htaccess file (note that ALL URLs of the right type will be redirected to index.php?page=*, not only my 6 page names from above): RewriteEngine on RewriteBase / RewriteRule ^$ index.php?page=blog RewriteRule ^([a-z0-9]+)$ $1/ [R] RewriteRule ^([a-z0-9]+)/$ index.php?page=$1 RewriteRule ^([a-z0-9]+)/([a-z0-9]+)$ $1/$2/ [R] RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/$ index.php?page=$1_$2 ErrorDocument 404 /index.php?page=status_404 All that is working like a charm, but then I wanted a good "404 page not found" system. If someone browse the non-existent pages (i.e. query strings of the form * and *_*) "/blog/tga/", "/blo/", "/something/else/" or whatever, they should be sent to a "404 error page". Apache's "ErrorDocument 404" won't catch these kind of wrong pages, because every query string belongs to the file index.php, which IS existent. What Apache's ErrorDocument 404 will catch, is URLs like "/blog/tag/archive/" or "/jdklasjk-ds", but these are less likely to be typed I reckon. I tested it, and the ErrorDocument part is working like it should. In order to catch these wrong query strings, I have the following PHP code at the very beginning of my index.php file: <?php $page_array = array("status_404", "blog", "blog_tag", "map", "map_country", "about", "about_me"); if (!in_array($_GET['page'], $page_array)) { header("HTTP/1.0 404 Not Found"); exit; } ?> And this should be working fine I think, but for some reason, when I type something my filter should catch, like "/blogg/" the page turns out blank. I know that the if-statement is actually being processed (through testing), and the reason the page turns out blank, is because exit; is reached. My problem is, that the header seams to be ignored. The weird thing is, if I change the header to a "header("Location: /status_404");" the header is NOT ignored, and you're redirected to my error page "/status_404". Some may be fine with this 'solution', but I'd like it to work like it should. This way I will have two kind of 404 errors; one where it's served through my .htaccess file (that's the right way - the error page is showed still having the wrong URL you typed in the address bar of the browser) and the other where you are redirected to /status_404 (this way you can't check the URL you just typed for misspellings or the like). Get my point? Hope someone can help me out here with this annoying problem. BTW I've tested the above on both my localhost (PHP 4.4.7/Apache 2.2.4 win32) and web hotel (PHP 4.4.7/Apache 1.3.37 unix), with same results. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted October 14, 2007 Author Share Posted October 14, 2007 Ahhh, found the answer myself. It's because Apache can't go back and do a 404 after it served the actual script (index.php) successfully. And I also found a workaround here: http://www.webmasterworld.com/forum88/12822.htm. Quote Link to comment 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.