Wednesday, April 19, 2017

domain name system - Rewrite URL but have php read old URL




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


Answer



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.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...