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

    Date: 04/08/05 (PHP Community)    Keywords: php, browser, css, html, web, apache

    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];
    		include('get.php');
    	}
    	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.

    Source: http://www.livejournal.com/community/php/281753.html

« Need REGEX help || ImageAntiAlias() »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home