How to automatically create virtual subdomains using PHP? - php

I really appreciate any answer to my question because I am searching for this about two weeks. My goal is to create directories using PHP and displaying them as a virtual subdomain (All procedure should have done automatically).
For example :
example.com/test/index.php should be considered as :
test.example.com/index.php

The only way I was able to do such thing was using a wildcard subdomain. If your server supports that, it's just a matter of using a front controller to manage the requests.

You cannot do this solely with PHP. You need dynamic shell scripting to create the DNS zone files for each subdomain.
EDIT:
Probably #Robyflc is right, you can base conditions on the host name in PHP. It is not clear form the question if you want the subdomain or just some logic like create a URL user1.domain.com and then find the folder depending on the value of it.

To do that, I recommand you to buy a VPS server
it's possible with URL Rewriting.

Related

Create a sub domain with only PHP (cross-host)

I have seen a lot of solutions that involve using .htaccess. I would like to know a way to create sub domains using only PHP. For my purposes, this also needs to work using non-host-specific mechanics so it would work on most hosts.
All I know is that I would need a wildcard CNAME record that says all sub domains point to x.x.x.x, but I don’t know what to do from here. What I think you need to do is create a folder that contains the code for the sub domains, and I have done this, but I cannot find a cross-host way to link the sub domain with the folder. There has to be a way to do this as I have seen it done, but I can not find a way that meets my needs.
The problem here is that PHP doesn't handle the request coming in, a web server (e.g. Apache or Nginx) does and it's the config for that software that determines where a request goes. Now the good news is that you can have wildcards in your config (at least for Apache and Nginx, YMMV if you're not using one of those), as long as you can access the config file (you'll need root access on the server). There is plenty of information available out there depending on what web server you are using so you can google that part.
Now, assuming you've done that part, in PHP you just need to check what the root domain for the request is. That information is stored in $_SERVER['HTTP_HOST'], so you can use a simple script to figure out which subdomain has been requested and then launch the appropriate script for that subdomain. Something like this should do the trick:
// Assuming request comes from https://subdomain.mydomain.com
$subdomain = str_replace('.mydomain.com', '', $_SERVER['HTTP_HOST']);
echo $subdomain // Outputs "subdomain"
This will capture multiple levels of subdomains as well, so if your request comes from https://sub1.sub2.subdomain.mydomain.com then $subdomain would contain sub1.sub2.subdomain.
Edit after comment
You can't do this with shared hosting. Basically the config panel you get with the host, when you set up a domain or subdomain it's modifying the config file on the server for you. There's no way a host would let anyone access the config through anything other than their control panel for security. It's possible that hosts will have a similar setup to your test server, where subdomains just work as they use wildcards by default, but I don't know that and there's no guarantee of that.
WordPress itself is just blogging software. It doesn't let you set up lots of separate websites with their own installations. What WordPress Multisite (I assume that's what you mean when you mention WordPress) is use a single installation to host multiple "sites", but it's still one installation, one single database. All of the posts, all the pages, they're all stored in a single database and if you got into that database you could easily include a page from one site on another just by editing some fields in the database. It's not designed for reselling or for multiple, completely separate entities. There are a whole host of security risks in doing that sort of thing. It's designed for single entities that want to split their sites up into multiple sites, but where it's all one company, or related companies. Universities with different departments is one example I read about before, each department has their own "site" and the main IT office has a super user that can access all of them as it's on one single installation.
I could do with a little more information on what you're trying to do, but it sounds like you're trying to do something like WPEngine, where they sell hosting space and install WordPress for you. But they have dedicated servers that run scripts that create the config files and install WordPress on your own individual hosting space using their servers. That's known as SaaS (Software as a Service) and from the little you have said seems what you are trying to do. People subscribe to your site and get their own instance of the software you're selling that they access through an admin portal. That's not something they can install on their own hosting, they have to use yours. That's how most companies do this sort of thing.

Handle multiple sites through subdomains

I'm trying to improve my skills in PHP by creating my own CMS infrastructure, probably based on a lightweight framework like FuelPHP.
Because I think it's better to have a goal, I would like to use this opportunity to build a multi site CMS.
It would be a CMS for a game.
Basically, here the workflow:
Somebody create an account on the main site (http://www.mainsite.com), then create a site for his server (http://my-beautiful-server.mainsite.com). People from his server would then come to the subdomain and register to interact with the forum, the gallery, updating their account, etc.
Is using subdomains a correct approach ?
If I use a wildcard for my DNS, how must the vhost be configured to redirect on the correct folder based on the subdomain ? Can I not use a folder system for each site and use only one app to handle everything by just "passing" the subdomain as a variable to the app to tell it on which site I am ? Is it reliable ?
If in the future I want to propose custom domain names, is it possible with this approach ? I would only need to handle domain name registering myself and add a vhost configuration to redirect on the correct folder ?
I'm a little lost on the correct and most robust approach.
Today I have the domain name and I will take a little VPS for testing.
Thanks a lot for any help to guide me in the good direction and sorry for my broken english, I had trouble finding ressources on the Internet.
Is using subdomains a correct approach ?
It is a perfectly acceptable approach.
If I use a wildcard for my DNS, how must the vhost be configured to redirect on the correct folder based on the subdomain ?
You use a default vhost to capture all the requests for the different hostnames.
Then you can shuffle them around using mod_rewrite or by examining the requested hostname at the application level.
Can I not use a folder system for each site and use only one app to handle everything by just "passing" the subdomain as a variable to the app to tell it on which site I am ? Is it reliable ?
Yes.
The variable will already be available to your PHP via $_SERVER['HTTP_HOST'].
If in the future I want to propose custom domain names, is it possible with this approach ?
Yes. There is no practical difference between a domain name and a subdomain as far as a webserver is concerned.
I would only need to handle domain name registering myself and add a vhost configuration to redirect on the correct folder ?
You wouldn't even need a specific vhost configuration. Your default one would suffice.

How to store tags in the url without a "?"

I'm wondering how I can store tags in a url without the ? and without keys.
eg. Google Product forums does this: http://productforums.google.com/forum/#!topic/analytics/8qtK861a0iU
Each topic wouldn't have it's own directory and so a server side script determines which topic to show visitors.
Does anybody know how it is possible to do this with PHP?
Thanks
Google use a range of software to do this. one of which, can be done using .htaccess used on Apache servers (not IIS)
This is called modrewrite. A URL below can show an example of such tasks:
http://roshanbh.com.np/2008/03/url-rewriting-examples-htaccess.html
Do start learning this, upload a file called .htaccess to your Apache driven webserver.

Using one site for multiple domain in PHP

I have one site completely working for one client , now I have some more clients want same thing replicated for them , Is there any way , that I can use this site as base site , as I plan to access this site from there domain and providing database for each client.
I am using PHP and MYSQL.
Thanks for any support , I appreciate your point of view also in this process. do I have right approach
I have been told that there will be SEO issues if I use one site for multiple domain. I have no competent person available which can direct me on domain name linking. I have www.xyzuniversity.com and 85% data is fetching from database. now i have to create abcuniversity.com and I want that I just create new database and ready to use and I think I can make multiple sites like this , if I succeed
Thanks
You can point multiple domains to the site. You can get the domain name from the server vars ($_SERVER['HTTP_HOST']) and choose you database through that. Make sure you dont have any absolute links.
Put the shared code into a shared directory and give each domain it's own database configuration, so the database configuration is not shared.
Then start your application with the different configuration based on the domain, e.g. by server environment variables like the hostname.
If your design does not support a configuration that can be injected into the application, you need to maintain two code-bases, one for each domain, e.g. in source-code control with one branch per domain.
However I suggest that your code is that modular that it supports configuration for the parts that need configuration according to your needs. If it's not yet, think about coming closer to it and make the changes.

How to dynamically create subdomains

I want to dynamically create new sub domains on my website. i mean if you have example.com then you dynamically create sub.example.com .
i looked on google and there was nothing really good. i downloaded some scripts but they were all not working. my website is based on php so please if there is anyway i can do it in php tell me, and also if it can be possible with python then that is awesome!.
You need to setup a wildcard subdomain. Basically, anything.mysite.com will resolve to mysite.com. At that point it's up to your code to kick in and serve up the proper content depending on what subdomain the user is looking at.
http://www.google.lk/search?q=wildcard+subdomain
You can't do this unless you have permission to alter the DNS server for your domain.

Categories