Jump to content

[SOLVED] 404 header ignored (not the usual question)


thebadbad

Recommended Posts

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.