Custom Domain mapping in cloud based application - php

I am creating a cloud based application in laravel/php.
I have a functionality where user can have their website at user.myapp.com and it works (with subdomain and model binding).
I now want to go one step further, and allow users to add their own custom domain names. So their website will be reachable at user-domain.com rather than (or as well as) user.myapp.com.
I want to understand how to do this. I'm using Laravel framework which is based on php and apache.
What kind of server do I need? Can I use shared hosting?
What configs do I need to do in order for this to work?
What DNS record the user must create to put things together?

You can do this with any kind of server. You can make a route that is caught by any domain and in the controller of this route, you can query the database to know what website should be displayed for that domain.
You can get the domain name of the current request using app('request')->root();

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.

Point Sub Domain to another Domain

We are creating a SAAS base application, where every company get its own subdomain. But if the company wants to configure another domain with our application, they can do that.
e.g. I am a company registered with the application www.mysaas.com, i got the subdomain "company.mysaas.com", now i want to point my "company.com" domain to the "company.mysaas.com", so how we can achieve this?
We want to create a custom domain configuration settings for the users of our application. Its like wordpress domain mapping (you can point your own domain to the wordpress site). So as i read related with wordpress they asked to add the CNAME of your site for the domain which you want to configure for mapping.
Our application is written in Codeigniter.
Is there any server configuration changes we need to do to achieve this?

Building a SaaS application in CodeIgniter like Shopify with custom domain alias

I am working on a SaaS application. We take Shopify / Magento Go etc as reference.
I used CodeIgniter PHP framework.
SaaS Methodology - Multiple database, single code-base
Our SaaS architecture is as follows:
when a customer signup a database is build with his name and contain his specific databases.
when a customer signup he get a sub-domain (Ex: test1.example.com)
The customer can add his own domain that points to the system defined sub-domain. (Ex: test1.example.com to www.mydomain.com )
How to achieve this functionality?
For this you have to go through few configuration options.
Configure DNS: Create sub-domain with *. (ask you host)
You have to configure apache or .htaccess to point all requests coming to your domain point to same directory.
Database Setting: Try to learn how to use multiple database in codeigniter.
That's all. Enjoy!
I think that you should look for the term: " codeigniter multi Tennant app " and as said before, managing too many database isn't a solution!
You can achive proposed SAAS application this way,
There will be 1 database for application itself, and another N number of databases for clients, each new client will have new database.
Allow signup to your clients by asking basic details + subdomain to choose like blogspot do, subdomains taken by existing clients wont be allowed to reuse
When client signup successfully, one separate DB will be created and they will landed to their dashboard. Here one DB server will be fine but should have enough space and other parameter set to handle multiple databases.
Now dahsboard onwards, user will able to see list of functionality to use along with some predefined set of content that will be shared between user and loaded from "app DB". Whatever they do add will goes to their own independent DB
Here codebase will be deployed at one code server and main domain will be mapped to that so login/singup etc will be happen here and user's identification will happen at login level.
App DB: This DB will be responsible to store app level master data, i.e. Client's profile + login details, other admin related stuff which they meant to share to their clients. In case of shopify kind of app, themes, layout, templates can be master stuff that will store into this DB.
User DB: Only user's created, added content and its sub content should be stored. Whatever we can call user's ownership should be stored here.
Note: If its paid service, then you can use good payment gateway like stripe or chargebee to manage all your plans, subscriptions, email notification, dunning, payments log, payment success/failure, payment invoices.
WRT to subdomain and custom domain concept, you can do this using virtual hosting mechanism, you can do with apache as well as nginx server.
- Subdomain: Create config files as per server, put on proper place and any created subdomain will bind to particular pysical virtual directory
- Customdomain: Can create physical directory same as subdomain along with config files but in this case, you will need to ask your client to do DNS binding with our IP addess
In both this case when request comes to our server, server will check if config file and physical directory available tp hit then it will redirect user to destiny forlder.

Building a SaaS application on Zend Framework

I am on the way of building a SaaS application using Zend Framework on PHP. Here is the basic information of the project. Its Project Management System on SaaS model. When the user registered on the site, they will get a domain name like the format:
user_name.pms.com
user_name - The chosen User Name when the user registered on the Project Management System (pms)
pms.com - is the main SaaS Server.
Currently we provide the Sub Domain on our Server. And for big firms, it won't look good and they may prefer to get their own domain instead of this default one. So there may be:
user_name.pms.com [Always Present]
user1.com [ The custom or independent domain of the User ] This custom domain needs to link into the default user url.
The full website is planning to develop using Zend Framework.
On Zend Framework, we are having following HTAccess on the root folder for its working and is below:
SetEnv APPLICATION_ENV development
RewriteEngine On
RewriteRule .* index.php
Here are my questions:
Do I really need to create Sub Domain like profile url for all users once they registered on the site ?
If it need to create Sub Domains, can PHP check whether the chosen sub domains exists or not and can create Sub Domains from the Script itself ?
If there is no need to Sub Domain, can we achieve the same goal using HTAccess in Zend Installation ?
Can you provide the HT Access code which does the following:
"user_name1.pms.com" need to redirect to "pms.com"
"user_name1.pms.com/contact" need to redirect to "pms.com/contact"
ie any request on the Sub Domain like URL must redirect to main website with the format: pms.com followed by Query String.
Very Important:
Important 1:
When I said the redirect to main website, I need to keep the sub domain like URL on the address bar but the request will be served by main website.
ie "user_name1.pms/contact" is going to be served from pms.com/contact but on address bar we still see the url "user_name1.pms/contact"
Important 2:
Whenever we uses the HTAccess to redirect the request to main Zend Server, can I identify the actual url entered on the address bar ie "site1.pms or site1.com" ?
Another Question of Custom Domain Redirection:
The custom domain name like "site1.com" or "site2.com" need to redirect to either:
Option (a): "pms.com"
Option (b): "site1.pms.com
to serve the request. Here also, I need to keep the url on address bar same like the one user entered.
Which above option is better (a) or (b) ?
Which technology does this work, domain mapping or CName ? Or Any other technology for make this working.
There are several ways to get you at least near to what you want... but to get all of the way there, you're going to need to get a little more depth.
First of all, your questions:
You may need to actually set up a whole virtual host for each user, if you intend for them to be able to interact with your site through their own domain. If you just want them to have their domain redirect to yours that can be done at their registrar (and if they can successfully use masking, which I've always found problematic, it may work completely without doing this. If you ignore the custom domain requirement, then you can manage subdomains completely through mod_rewrite without having to actually set them up.
Probably the best architecture for your site is to proactively set up whatever needs to be set up the moment that the user signs up. Don't try to do it "just in time" when the user first tries to access it, and therefore it's just as simple as displaying an error when someone, say, types in usre.pms.com rather than user.pms.com.
You can use htaccess, or the config file for your site (which is better for performance, but just strictly for "getting done" purposes, htaccess will work fine).
Google
Very Important Point #1: this is how mod_rewrite works. No worries.
Very Important Point #2: yes, so long as you include that information to be passed along in your mod_rewrite rule
Your Last Question of Custom Domain Redirection:
This is where things get complicated. You can't serve site1.com from pms.com without apache being fully aware that it's looking for site1.com (unless you get forwarding with masking to work without issue at the registrar). Generally speaking, if you're using forwarding of some sort, then you'll want them to forward to the subdomain, and all will be good completely through mod_rewrite. If they are directing the domain straight at your server, a CNAME record is probably the right choice, directing to the subdomain, but you'll still have to learn about virtual hosts and how to properly set it up to get everything to work.
I think you may be signing on for more than you realize by letting your clients have their own custom domain. You can learn how to do what you want with subdomains probably in an afternoon or a couple days tops. Figuring all of the ins and outs to working with custom domain names could take a lot longer.
The sub domains can be handled using some technique in CPanel. To add vurtual sub domains from CPanel, do the steps:
Select "CPanel - Sub Domains"
Enter star and choose your domain name
Choose the directory to which you need to redirect
And handle the redirection from your page.
To develop a SaaS application using Zend, check the tutorial SaaS application development using PHP in Zend Framework

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.

Categories