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 was given a VM at my company to install web software on. But I came across a rather bizarre issue where PHP variables would be overwritten (rewritten) by the server if they matched a specific pattern. What could rewrite PHP variables like this?

The following is as an entire standalone script.

foo = '';
echo $foo; // ''

$bar = '';
echo $bar; // ''

$var = '';
echo $var; // '' (not overwritten because it didn't match whatever RegEx has been set)

Essentially any variable which contains a subdomain and matches on the domain name would be rewritten. This isn't something mod_rewrite would be able to touch, so it has to be something at the server level that is parsing out PHP and rewriting a string if it matches a RegEx.

share|improve this question

migrated from Jul 25 '12 at 17:41

This question came from our site for computer enthusiasts and power users.

Is this really how your script is laid out? I mean, you echo the variable right after the variable declaration and you get this result? I am asking because your example, as posted is impossible, at least with the php I am used to. – stefgosselin Feb 16 '12 at 4:45
You might want to check your php.ini file to look for any sort of settings that might have to do with domains or URL settings? Haven't seen something like this, before. Like @stefgosselin said, is this your whole script? Nothing else is being included, right? – summea Feb 16 '12 at 6:15
Yes, this is also the entire script. No files included. I am not familiar with anything that could do this. But it is. – Highway of Life Feb 16 '12 at 19:13
up vote 1 down vote accepted

Output overwriting is possible within Apache by using mod_perl: PerlOutputFilterHandler.

The following could be added to an apache.conf to set the output filter:

<FilesMatch "\.(html?|php|xml|css)$">
    PerlSetVar Filter On
    PerlHandler MyApache2::FilterDomain
    PerlOutputFilterHandler MyApache2::FilterDomain

Example filter handler code:

package MyApache2::FilterDomain;

use strict;
use warnings;

use Apache2::Filter();
use Apache2::RequestRec();
use APR::Table();

use Apache2::Const -compile => qw(OK);

use constant BUFF_LEN => 1024;

sub handler {
    my $f = shift;
    my @hostname = split(/\./, $f->r->hostname);
    my $new_hostname = $hostname[0].".".$hostname[1];

    unless ($f->ctx) {

    while ($f->read(my $buffer, BUFF_LEN)) {
        $buffer =~ s/([a-z0-9]+)+\.domain\./$new_hostname\.$1.domain\./g;   

    return Apache2::Const::OK;

More on Apache mod_perl filters can be found here: mod_perl: Input and Output Filters

share|improve this answer

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.