Another Decker (another_decker) wrote in php,
Another Decker

PHP, mod_rewrite and header() - This is a good one.

I'm trying to use mod_rewrite to intercept all requests made to my web server and route them to a php file (index.php) which then parses the request and does what it can to serve the appropriate page. I'm doing this in an effort to use meaningful URLS as opposed to query strings.

I have everything about half-working but I can't send headers from index.php, which is important because not only do I need to be able to generate 404 errors, but the files that will be included by index.php need to set their own headers (content-type, content-language).

These are the lines I'm using in .htaccess for mod_rewrite:
RewriteEngine on
RewriteRule index.php$ - [L]
RewriteRule !\.(jpg|gif|css)$ /index.php [L]

And this is the code I'm using in index.php that's causing a problem:
if ($url_array[0] == 'poems') { // POEMS SUBDIRECTORY
	require('lists.php'); // GET LISTS
	$get = 'poems';
	if (array_search($url_array[1], $poem_list)) {
		$which = $url_array[1];
	else {
		if (!headers_sent()) {
			header('HTTP/1.1 404 Not Found');
else {
	if (!headers_sent()) {
		header('HTTP/1.1 404 Not Found');

As you can see, I'm checking to see if headers have been sent before trying to set them, but they don't seem to have been. Whenever I try to set a status header with the syntax HTTP/1.1 XXX (description) an internal server error occurs. I have been able to set status using the header Status: 404, but rather than the standard 404 error page appearing a blank HTML document is served to the browser.

Apache version is 1.3.19 running on Windows 98. PHP version is 4.2.3 running as a cgi.

Any help would be much appreciated, I'm pretty far out of my depth with changing the way Apache works.

Edit: While the reason for the header being rejected was not discovered, I have found an acceptable alternative. Many thanks to those that weighed in on my problem.
  • Post a new comment


    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.