Welcome to WebmasterWorld Guest from 54.205.251.179

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

How do I configure Apache2 Virtual Hosts using %HTTP REFER

Apache2 Virtual Host Config

   
9:16 am on Jul 27, 2012 (gmt 0)



I have two domain names e.g. www.site1.com and www.site2.com. I have a static ip address e.g. 11.123.456.321

All requests for either domain return 11.123.456.321 as the %HTTP_HOST while %HTTP_REFERER contains the domain name requested.

How do I persuade the VirtualHost ServerName to = %HTTP_REFERER ?
5:36 pm on Aug 2, 2012 (gmt 0)



I don't understand. If Apache is differentiating between
www.site1.com
and
www.site2.com
, then the
ServerName
directives for each virtual host should already be set to the appropriate values.

--
Ryan
5:56 pm on Aug 2, 2012 (gmt 0)



That's the problem. Apache looks at %HTTP_HOST and it is always the static IP address. %HTTP_REFERER holds the web site name requested. e.g. www.example1.com and I want a way to move the contents of %HTTP_REFERER into %HTTP_HOST before the virtual host is assigned by Apache so that it goes to the right site. Sorry if I am not being clear.

Tks

Dave
8:01 am on Aug 3, 2012 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, dtaylor7!

%HTTP_REFERER should hold the value of the HTTP Referer: header sent by the user agent.
i'm not sure how you could see a Referer: header for a direct request.

do you have access to your server configuration file?
(typically named something like httpd.conf)
11:08 am on Aug 3, 2012 (gmt 0)



I am using Ubuntu so httpd.conf is not used, instead everything is configured in:
apache2.conf +
ports.conf (which contains the NameVirtualHost *:80 directive)
+ mods-enabled dir
+ sites-enabled dir

In the sites-enabled dir the 000-default file includes the following Virtual Server directives:

# Virtual Host Directives
ServerAdmin me@example
<VirtualHost *:80>
ServerName www.example1.co.uk
DocumentRoot /var/www/example/example1dir
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory "/var/www/example/example1dir/">
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
Allow from all
AuthName "Welcome to the Example 1 Website. Please Log In"
AuthType basic
AuthUserFile /var/www/.example1htusers
require valid-user
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
#LogLevel warn
Loglevel debug
CustomLog /var/log/apache2/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>
# Second Host
<VirtualHost *:80>
ServerName www.example2.co.uk
DocumentRoot /var/www/example/example2dir
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory "/var/www/example/example2dir/">
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/log/apache2/cbeherror.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
#LogLevel warn
Loglevel debug
CustomLog /var/log/apache2/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>

If I do a get for the following index.php file via: www.example1.com/index.php

<?php
$headers = apache_request_headers();

foreach ($headers as $header => $value) {
echo "$header: $value <br />\n";
}
?>

I can see that %HTTP_HOST
    ALWAYS
contains the static IP address that my DNS provider redirects to e.g.

101.202.303.404

On the other hand %HTTP_REFERER contains:

www.example1.co.uk or www.example2.co.uk depending on the site requested.

I think Apache is looking at %HTTP_HOST to decide which virtual server to assign.
It never finds a match and therefore defaults to the first Virtual Server

i.e. www.example1.co.uk

So, if I can manipulate the Header to put the contents of %HTTP_REFERER into %HTTP_HOST
before Apache checks the ServerName then I think I should get the right VirtualHost assigned.

I tried asking the DNS provider for some help. Because they don't host anything on
my behalf they won't even look at it........

Many thanks for your help.
12:15 am on Aug 4, 2012 (gmt 0)



OK, so your actual problem is that requests for both
www.example1.co.uk
and
www.example2.co.uk
appear to be handled by the first VirtualHost block. And this appears to be happening because of some redirect that removes the Host header from the request causing Apache to set HTTP_HOST to the server's IP address.

The expected host/domain, however, is available in the request's Referer header and, therefore, Apache's HTTP_REFERER variable, and you're wondering how to set HTTP_HOST to HTTP_REFERER before Apache checks for a matching virtual host.

Honestly, I have no idea—sorry—but it sounds like your problem is further up the chain: the redirect. I'm not sure why your "DNS provider" would be redirecting requests. That's not how DNS works...

--
Ryan
2:40 am on Aug 4, 2012 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



ports.conf (which contains the NameVirtualHost *:80 directive)

I say this with trepidation because, as we all know, I don't speak Apache, but isn't that where you need to look for the source of the problem? By the time you get to sites-enabled the damage has already been done.