Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an issue involving PHP and mod_rewrite.

I am trying to load the page



// get requested uri
$uri = explode('/', strtolower(substr($_SERVER['REQUEST_URI'], 1)));


// route current page
if ($uri[0] == '1'){
    $page_id = '<br />1';
} else {
    header("HTTP/1.0 404 Not Found");

echo $page_id;


The var_dump gives out string(1) "1" and the page_id echoed is 1.

The problem is that I also get the following warning.

PHP Warning:  Cannot modify header information - headers already sent by (output started at PATH:6) in /PATH on line 12

Line 6 is the var_dump.

Line 12 is the header line.

I should not be getting that warning since that code should not be getting run.


RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA]

IndexIgnore *
Options -Indexes virtual host

<VirtualHost *:80>
Redirect 301 /

<VirtualHost *:80>


DocumentRoot PATH/

LogLevel warn
ErrorLog  /var/log/apache2/
CustomLog /var/log/apache2/ combined


Some things I've tried.

When I remove the .htaccess file and replace 1 by index.php and try to access testwebsite/index.php I don't get the error.

If I set the $uri[0] = '1'; manually I don't get the warning.

If I set the $uri = '/1'; manually I don't get the warning.

If I add more code to the else {, like set the page_id to something else, it doesn't do anything. It seems like only header lines get executed and produce a warning.

It seems like the mod_rewrite is somehow messing up the $_SERVER variable. Maybe some kind of encoding issue since the var_dump is normal.


share|improve this question

Seems a PHP error more than anything else.

If there is ANY output from a script, you can NOT do a header() function anymore. So

echo 1; header(...)

Will fail, because you sent the number 1 back to the client already. So your var_dump sends some output, and this causes the header() function to fail. Remove the var_dump.

share|improve this answer
Thanks for your reply. The header should not be executed because it's in the else statement. – Enkay Apr 9 '11 at 0:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.