Fixing Apache virtual host problems

Q In Apache, Virtualhosts aliases don't work using PHP, though they do work with TXT. Here's a part of my httpd.conf:

RewriteMap lowercase int:tolower
RewriteMap host-map prg:/var/www/hosts.php
RewriteEngine On
RewriteRule ^/icons/(.+) /var/www/icons/$1 [L]
RewriteRule ^(.+)
${lowercase:%{HTTP_HOST}}$1 [C]
RewriteRule ^(www\.)?([^/]+)/cgi-bin/(.*) /var/www/users/${host-map:$2|$2}/cgi-bin/$3
[T=application/xhttpd-cgi,L,E=VHOST:$2]
RewriteRule ^(www\.)?([^/]+)/(.*)
/var/www/users/${host-map:$2|$2}/$3 [E=VHOST:$2]

Here's the hosts.php:

#!/usr/local/bin/php -q
mysql_connect("localhost","iglou","Frosties");
$fdin=fopen("php://stdin","r");
$fdout=fopen("php://stdout","w");
set_file_buffer($fdout,0);
while($l=fgets($fdin,256)) {
fputs($fdout,key_lookup($l)."\n");
}
function key_lookup($key) {
$res=mysql_query("SELECT dir FROM iglou.web_aliases WHERE alias='$key' LIMIT 1");
if(@mysql_num_rows($res)) {
return @mysql_result($res,0,0);
} else {
return $key;
}
}

And the MySQL 'web_aliases' table in the database 'iglou' looks like this:

CREATE TABLE 'web_aliases' (
'id' int(6) NOT NULL auto_increment,
'ws_id' int(6) NOT NULL default '0', 'dir' varchar(50) NOT NULL default '',
'alias' varchar(50) NOT NULL
default '',
PRIMARY KEY ('id')
) TYPE=MyISAM AUTO_INCREMENT=2 ;
INSERT INTO 'web_aliases'
VALUES (1, 0, 'original-domain.net', 'alias-domain.net');

The problem is that when I try to access alias-domain.net it points to /var/www/users. What do you think is the problem?

A The first step for you to take is to run the PHP script from the command line, and pass it a domain name which you can then use to verify that is sending the correct information back to Apache. It may be that there is a hiccup with connecting to the database, or that the script is bailing out at some point. The fact that it returns nothing, rather than the original entry, suggests to me that it's not very happy with something in the database. We tested your script, and it worked. However, as we were building the database config from scratch, it's likely that we missed a problem existing in your configuration.

Follow us on Identi.ca or Twitter

Username:   Password:
Create Account | About TuxRadar