Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

How can I route users using a subdomain configuration in my URL (name.companywebsite.com)?

I'm updating an app that's using PHP Blitz and converting it to a straight forward PHP / OOP dynamic.

Right now, when the user hits the site, there's a subdomain configuration in place that looks like this:

erin.thecompanyswebsite.com

"erin" is being isolated and identified by this function:

function getSubdomain($url=NULL) {
    if ($url == NULL)
        $url = $_SERVER['HTTP_HOST'];
    $aHost = explode('.',$url);
    $sd = 'www';
    if (count($aHost)>2)
        $sd = $aHost[0];
    return $sd;
}

It works great, but it would appear that it works only if you've got some kind of configuration in place similar to Larvel or Symfony where the htaccess file has been set up in a specific way to accommodate that configuration.

And I'm speculating, so don't hear me as being overly confident.

Because of the number of users that are utilizing this dynamic where their name prefaces the url of the site, I've got to duplicate this, but I'm not sure how.

How?
Avatar of Damian
Damian
Flag of Australia image

Shouldn't you be doing this via CNAMEs in your DNS ?
Avatar of Bruce Gust

ASKER

You may be right.

Since I posted this question, I created a Wildcard in my DNS settings. I was using http://kbeezie.com/wildcard-subdomains-php/ as my guide. Right now, I'm in "stand by" mode - waiting for my changes to propagate. I don't know if what I did is going to work, though, because even if I successfully redirect my user to a specific URL. I still have to figure out how to grab the first part of the URL in order to have something to query the database.

In other words, if my URL is hank.brucegust.com. Seems like my Wildcard would direct that to some page, but I still have to be able to access the "hank" part of the URL and I don't know how I'm going to do that.

Thoughts?
Well, in terms of world DNS, like ftp.thecompanyswebsite.com or community.spiceworks.com the DNS would have to resolve from the outside, before traffic even hits the htaccess or site code, now there may be other methods of achieving this that I am unaware of, but regardless of that.. this very site (spiceworks) is testament to the fact that it can be achieved.

Essentially, www.spiceworks.com and community.spiceworks.com are both pointing to 192.230.84.70.

So, if you deal with this NS resolution at your hosts CPanel (or what ever kind of back end is running) then you should be able to map folders or redirect in such a way so as to accommodate the traffic that comes in, and from there you could populate those maps or redirected folders with your scripts.

"I don't know if what I did is going to work, though, because even if I successfully redirect my user to a specific URL"
Well, the good thing is your traffic, mostly speaking, won't auto-dial in hank.brucegust.com, they will (presumably) go to brucegust.com).  Essentially, DNS is read backwards in the eyes of network systems...so the computer actually sees "com brucegust hank", so "hank" should be a folder on your host or subdomain etc .  You really just need to make the provision for 'hank' on your host system.

It's a rather general round about explanation, but depending on what and where you have running, I think this philosophy should get you where you need to be.
Damian, I've got another "bead" on this thing and I think it's going to work real well.

I've spoken with the network administrator who did some digging and was able to determine that the dynamics that are processing the subdomains appear to be documented in the apache config file.

There may be more to it than this, but he's going to set me up with a dev server so I can kick the tires and see what's what.

So, never mind the Wildcard scenario and forget the htaccess approach. Here's what I believe will work - check my logic and tell me what you think.

The DNS setting is kind of like the Yellow Pages. When someone types in your URL, the server is processing it as an IP address and connects the user to that particular directory / content.

The Apache config file is kind of like the internal phone directory for a particular site. On the config file that I have, I've got this:


#########################
# mysite.net
#########################

<VirtualHost 192.168.2.106:443>
      ServerName mysite.net
      ServerAlias *.mysite.net
      DocumentRoot ../www/mysite.net/
      SSLEngine On

      Alias /js/              ../www/_common_/js/
      
      SSLCertificateFile conf/ssl/star_mysite_net.crt
      SSLCertificateKeyFile conf/ssl/star_mysite_net.key
      SSLCertificateChainFile conf/ssl/DigiCertCA.crt

      CustomLog "../logs/apache/access.mysite.log" common
      ErrorLog "../logs/apache/error.mysite.log"

</VirtualHost>

I'm thinking "ServerAlias" is my homerun in that it allows for that kind of scenario when someone types in myname.mysite.net. Without that "ServerAlias," the user would get an error. But because that scenario is addressed in my config file, I've got a dynamic in place that allows for / expects that kind of situation.

Yes?

Based on what I can see, it would appear that the URL: myname.mysite.net shuttles the user to the index.php that's sitting on the server and from there, I can use some PHP to grab the characters that precede the "."

Am I on to something, or what?
ASKER CERTIFIED SOLUTION
Avatar of Damian
Damian
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That's going to work, Damian!

Thanks!