Reputation: 53
I have done some research on this problem without and solution yet.
Setup as follows: Multiple domains say, www.domainA.com and www.domainB.com. Both domains are mapped a the same hosting folder on a LAMP server. I want the index.php file in that folder to 'know' which domain is accessing the files and vary content from SQL. I.e. load a different client ID specific to that domain.
I've looked into $_SERVER['REMOTE_HOST']
and $_SERVER['HTTP_REFERRER']
the latter has been clearly stated to be unreliable. I am after a sufficiently robust solution.
.htaccess
won't do the trick (I wouldn't think?) I was thinking something along the lines of domain DNS IP as a solid source for action. Would greatly appreciate some guidance.
UPDATE: There have been some solutions suggested - I would like to have an SQL table which would contain all expected domains. TABLE clients ('id_client', 'client_name', 'client_url') and query this table returning the 'id_client' to a $variable.
If $_SERVER['HTTP_HOST'] matches client_url in the database the corresponding id_client is returned. The .htaccess solution - however elegant - would require constant updating of the file. I'd prefer, where possible to keep solutions within the php and database.
What circumstances with $_SERVER['HTTP_HOST'] fail?
Upvotes: 2
Views: 387
Reputation: 621
This could be accomplished in two steps. First using the aliases in the vhost config for each domain you could point them such that all requests go to:
hosting folder/controller.php/Domain/requested folder/page
A vhosts file to accomplish this for domainA may look like this:
<VirtualHost *:80>
ServerName domainA.com
DirectoryIndex index.html index.htm index.php
DocumentRoot /var/www/
Alias /pages /var/www/pages/control.php/DomainA
</VirtualHost>
Then control.php would recieve all requests and Apache will sent the variable $_SERVER['PATH_INFO'] with everything after control.php. For example if the user goes to www.DomainA.com/folder1/pageC.php then $_SERVER['PATH_INFO'] would be '/DomainA/folder1/pageC.php'. Note that it is necessary for the dynamic pages to be separated into there own folder for this solution to work otherwise requests for static content will be directed through control.php causing problems. This directory structure will can be made invisible to the user if done carefully.
Control.php might then look like this for php pages:
<?php
if (isset($_SERVER['PATH_INFO']))
{
$path = explode('/', substr($_SERVER['PATH_INFO'], 1)); //substr removes leading slash
$domain = array_shift($path);
$page = implode('/', $path); //Note: you will want to verify that there is a page name and append 'index.php' if there is not.
}
require_once $page;
?>
Upvotes: 0
Reputation: 422
If you're looking to do this in your code then the easiest way is to check $_SERVER['HTTP_HOST']
which returns the hostname which the user is accessing your site using. For example, if I accessed http://www.exampleA.com/ then $_SERVER['HTTP_HOST']
would return www.exampleA.com
.
Then to do the routing it's a simple case of checking the host against a list of known hostnames and assigning the client_id based on that using a variable.
It may be slightly easier for you to use a framework like Zend Framework as it has this functionality already built in using Zend_Controller_Router_Route_Hostname but you should also be able to implement it relatively simply using just standalone PHP.
Upvotes: 1
Reputation: 679
You can indeed accomplish this with .htaccess
With modrewrite rewrite domaina.com to index.php?client_id=a
Here's an example of the code, pulled from http://corz.org/serv/tricks/htaccess2.php
#two domains served from one root..
RewriteCond %{HTTP_HOST} domain-one.com
RewriteCond %{REQUEST_URI} !^/one
RewriteRule ^(.*)$ one/$1 [L]
RewriteCond %{HTTP_HOST} domain-two.com
RewriteCond %{REQUEST_URI} !^two
RewriteRule ^(.*)$ two/$1 [L]
Just modify the regular expressions and routing to suit your situation.
Upvotes: 3