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?
brucegustPHP DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DamianIT incCommented:
Shouldn't you be doing this via CNAMEs in your DNS ?
0
brucegustPHP DeveloperAuthor Commented:
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?
0
DamianIT incCommented:
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.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

brucegustPHP DeveloperAuthor Commented:
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?
0
DamianIT incCommented:
"the dynamics that are processing the subdomains appear to be documented in the apache config file."

Yeah, as long as you deal with, as you say, "Without that "ServerAlias," the user would get an error. But because that scenario is addressed in my config file".. then you can catch all and direct traffic where you want.  I'm not much of an apache programmer, but I have configured a stack of DNS configs for many web developers, and I've seen this executed successfully before... just a little foggy on exactly what they did once we created aliases for it.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
brucegustPHP DeveloperAuthor Commented:
That's going to work, Damian!

Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.