Guys!
I will explain what I want to do. If anyone can help with this, please, give some advices.
I have a website, let's call it http://mysite.com. And subdomain, let's say http://myname.mysite.com.
There is one configuration file inc.php with some personal data each for mysite.com and myname.mysite.com, example ICQ number. All general website files located in mysite.com directory.
I want to be able all general files of the mysite.com at the subdomain myname.mysite.com, in order not to create completely similiar files at subdomain. But with including individual configuration file, located at the myname.mysite.com, containing individual ICQ number.
This is the example how I want this to work:
Main domain "mysite.com" has root folder "public_html" with pages: index.php, about.php, articles.php and configuration file inc.php with icq number "111-111". So when serfing "mysite.com" website, you see there ICQ number from inc.php.
Subdomain "myname.mysite.com" has folder "public_html/subdomain/myname" with only a configuration file inc.php with ICQ number "222-222". So when going to myname.mysite.com you will see the web pages from "mysite.com" but the ICQ number will get from here "public_html/subdomain/myname/inc.php". And in address bar you will see web pages as: http://myname.mysite.com/index.php http://myname.mysite.com/about.php http://myname.mysite.com/articles.php
So visitors will think that they are serfing individual website myname.mysite.com, but in real, visitors will serf on the mysite.com webpages. So if I change content on mysite.com, this content will be changed on myname.mysite.com.
I need all this staff cause I want to organize affiliate programm with giving personal subdomain name to my partners but with personal configuration file inc.php, that will take ICQ number for consulting.
I guess I've tried to explain what I want to do. God, do hard to explain this situation. Ah, forgot to mention, all this I want to do on PHP.
If the only thing you want changing is the inc.php
file, I would suggest structuring it slightly differently (the code below assumes only 1-level subdomains, such as subdomain.mysite.com, and some modifications in points (3) and (4) will be needed for deeper subdomains, such as sub.subdomain.mysite.com).
1) In your apache virtual host configuration, define all subdomains to be aliases for the main domain:
<VirtualHost *:80> ... SernarName mysite.com ServerAlias *.mysite.com ... </VirtualHost>
2) Have all your PHP files for all subdomains in one directory and point your apache config to that dir.
3) In that directory, create a bunch of inc
files: inc.php
, inc-subdomain1.php
, inc-subdomain2.php
and so on.
4) Have some sort of config.php
file, which you include from all other files in your site, and have it contain something like this:
if(isset($_SERVER['HTTP_HOST'])) { $host = explode('.', strtolower($_SERVER['HTTP_HOST'])); if(count($host) == 3) { //have something more than just mysite.com $subdomain = $host[0]; if($subdomain == 'www') { //www.mysite.com is the same as just mysite.com $subdomain == ''; } } else { //if just mysite.com, then no subdomains $subdomain = ''; } } else { //if no http_host info exists, then assume it's just mysite.com $subdomain = ''; } $incfile = 'inc' . ($subdomain == '' ? '' : "-$subdomain") . '.php'; if(!file_exists($incfile)) { //if no include file for the requested subdomain, just use mysite.com $incfile = 'inc.php'; } require_once($incfile);
Now, all of this, of course will give you a lot of flexibility, but may not be necessary if all you need is just the different ICQ numbers. In that case, I would change point (4) code to have only 1 include file, say inc.php
, and in that file have something like this:
$icq_default = '111-111';
$icq_nums = array('subdomain1' => '222-222',
'subdomain2' => '333-333',
...);
if(isset($_SERVER['HTTP_HOST'])) {
$host = explode('.', strtolower($_SERVER['HTTP_HOST']));
if(count($host) == 3) { //have something more than just mysite.com
$subdomain = $host[0];
if($subdomain == 'www') { //www.mysite.com is the same as just mysite.com
$subdomain == '';
}
}
else { //if just mysite.com, then no subdomains
$subdomain = '';
}
}
else { //if no http_host info exists, then assume it's just mysite.com
$subdomain = '';
}
$required_number = isset($icq_numbers[$subdomain]) ? $icq_numbers[$subdomain] : $icq_default;
If there is really only going to be one (or just a few) tiny setting(s) that is/are different across subdomains, you should probably consider serving all the subdomains from the same document/web root. Do this with a tweak to your vhost configuration. In Apache it would look something like this (assumes NameVirtualHost *:80
):
<VirtualHost *:80>
ServerName mysite.com
ServerAlias *.mysite.com
DocumentRoot /var/www/websites/mysite
</VirtualHost>
Then from within your code, you can determine which settings to use (or override) using the subdomain, which might be determined like so:
$domain = !empty($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
$subdomain = implode('.', explode('.', $domain, -2));
Then use the $subdomain
to determine which settings to use.
I am not going to supply example code for how you might determine which settings to use given the subdomain, because your implementation choice is very subjective to your needs.
EDIT - in response to the question from OP in first comment
If you absolutely can not put ServerAlias *.mysite.com
into your vhost configuration then there might still be an easy way to get things to work similarly anyways: Use symlinks for the document/web roots.
So if myname.mysite.com has the document/web root of /var/www/websites/myname.mysite.com and mysite.com has the document/web root of /var/www/websites/mysite.com
Then make /var/www/websites/myname.mysite.com a symlink to /var/www/websites/mysite.com on the web server:
cd /var/www/websites/
ln -s mysite.com myname.mysite.com
This should cause the files used/served by Apache for myname.mysite.com to come from the same location on the server as those for mysite.com.
EDIT - for cPanel
Okay, I am now convinced that cPanel is lame.
If you already have mod_proxy enabled, you could do this in each root .htaccess
RewriteRule ^([^/]*)(/?)(.*) http://mysite.com/$1$2$3 [L,P]
Of course this does not even consider requests coming in through https. You could avoid the use of mod_proxy by directing all requests to a symlink in the web/document root for myname.mysite.com, BUT that brings us back to the issue of creating symlinks. It might be easier to get symlinks in the document/web roots rather than making the document/web roots symlinks themselves.
I hope this has given you some ideas. It seems like you will need to do something beyond what cPanel provides to get things working how you want them to.