Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have an issue involving PHP and mod_rewrite.

I am trying to load the page testwebsite.com/1.

index.php

<?php

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

var_dump($uri[0]);

// 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.

.htaccess

RewriteEngine on

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

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

IndexIgnore *
Options -Indexes

testwebsite.com virtual host

<VirtualHost *:80>
ServerName www.testwebsite.com
Redirect 301 / http://testwebsite.com/
</VirtualHost>

<VirtualHost *:80>

ServerName testwebsite.com

DocumentRoot PATH/testwebsite.com/public

LogLevel warn
ErrorLog  /var/log/apache2/testwebsite.com-error.log
CustomLog /var/log/apache2/testwebsite.com-access.log combined

</VirtualHost>

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.

Thanks.

share|improve this question

1 Answer 1

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

 
discard

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.