1

In my platform, each user has their own config file. The file is named by the subdomain they created.

Example:

user1.domain.com
user2.domain.com
user3.domain.com

The system reads the url via $_SERVER['SERVER_NAME'] and grabs the subdomain. It then looks up the appropriate config file based off of the subdomain.

Example:

if the url is user1.domain.com the system looks up user1.config.php.

Each users has the option to use there own domain. I am currently doing this by pointing the A record.

Example:

user 1 points theirDomainName.com to my IP address via their A record

how can I use htaccess so the url reads theirDomainName.com but backend of the platform (php) reads user1.domain.com thus the platform knows to pull the user1.config file

  • You'll need to map it one place or another so you might as well just map them in the main PHP script. – Julie Pelletier Jun 17 '16 at 18:05
  • Can you explain? – Jason Jun 17 '16 at 18:08
  • Make an array in PHP that maps the domain name to the username so you'll know how to do your include. If you're afraid of correcting the existing script beyond that, you could even update $_SERVER['SERVER_NAME'] based on it. – Julie Pelletier Jun 17 '16 at 18:24
  • Possible duplicate of How to create a CNAME for a domain's root name – Sherif Jun 17 '16 at 18:37
  • Your question is basically asking "How do I create a CNAME for theirDomainName.com" whch is a duplicate of serverfault.com/questions/62527/… – Sherif Jun 17 '16 at 18:37
0

Instead of making rewrite rules in .htaccess, it would be much simpler to maintain by doing the mapping inside your PHP script.

That array should map the domain name to the username so you'll know how to do your include. If you're afraid of correcting the existing script beyond that, you could even update $_SERVER['SERVER_NAME'] based on it.

You could, for example, do: 'user1.domain.com', 'domain2.com'=>'user2.domain.com', 'domain3.com'=>'user3.domain.com'];

if (!array_key_exists($_SERVER['SERVER_NAME'], $clients)) {
    header('Location: http://domain.com/invalidclient');
    exit;
}

$_SERVER['SERVER_NAME'] = $clients[$_SERVER['SERVER_NAME']];

While it is not in the best practices to overwrite super globals, nothing prevents it and it gives you a really simple solution.

  • Thank you Julie. My brain was stuck and I needed that to get out of my block. :) – Jason Jun 17 '16 at 21:35
  • My pleasure! :) – Julie Pelletier Jun 17 '16 at 21:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.