3

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.saas.com, i got the subdomain company.saas.com, now i want to point my company.com domain to the company.saas.com, so how we can achieve this?

i know it can be done using Cname but is there a way to dynamic update Cname?

Naveen
  • 35
  • 2

2 Answers2

0

This depends entirely on how your application performs the hostname detection. E.g. if it runs on HTTPS, you'll need to add a virtual host on your web server. We could deduce, based on that you currently use subdomain.saas.example.com and the software is able to detect that, that this should be possible. You'll just use the same method for hostname identification than with the current solution.

On DNS level you should use an A record pointing to the same IP address. That's because using a CNAME record only causes another DNS lookup for the company.saas.example.com IN A record and it doesn't cause any change in the hostname detected, so you wouldn't get any benefit from using it; i.e. it's not a shortcut for preventing configuration of this hostname detection method.

If you wish to use CNAME (for example if the IP address for *.saas.example.com may change without acknowledging the customer), you should advice using a subdomain, e.g. saas.example.net instead of example.net. This is because when using a CNAME record you are not able to use any other records on the same hostname. With the domain name itself this means you lose the ability to add MX, TXT, NS etc. records. That would prevent using the same domain for email etc.

Esa Jokinen
  • 43,252
  • 2
  • 75
  • 122
  • What kind of server configuration i need? Do i need dedicated server or it can work on shared hosting as well? I am using apache and codeigniter/php. – Naveen Jun 20 '17 at 12:26
  • Share your Apache configuration for `*.saas.example.com` by adding it to your question. Then, I may help you to add an example virtual host for a custom domain. If the domain isn't always a direct match against the subdomain, you'll need to add one new virtual host for every single domain. But if you can sell a SaaS solution to a company, the effort:benefit rate in that would still be excellent. – Esa Jokinen Jun 20 '17 at 12:32
  • I am not sure how i can check Apache configuration. I am on shared hosting. Do you think we need dedicated server ? – Naveen Jun 20 '17 at 13:37
  • If you can't access Apache configuration, you'll need to ask for an addition every time a new domain appears. (Possibly you can do this via some web control panel, but they are off-topic here.) Therefore, a dedicated server (or VPS) would be ideal for this. – Esa Jokinen Jun 20 '17 at 13:47
  • So if i have dedicated server, how should i create virtual host? – Naveen Jun 22 '17 at 08:01
  • Basically you'll add a new `` section with `ServerName saas.example.net`. It can contain the same settings than a `company.saas.example.com` or be a redirect, depending how your software does the recognition. See [`VirtualHost` Examples](https://httpd.apache.org/docs/2.4/vhosts/examples.html). In some distributions virtual hosts are separate files in `/etc/apache2/sites-available/`, enabled with command `a2ensite`. – Esa Jokinen Jun 22 '17 at 08:06
  • I need a dynamic solution. i dont want to update that file regularly. is that possible? – Naveen Jun 22 '17 at 08:09
  • If you need to add these regularly and many, [mod_macro](https://httpd.apache.org/docs/2.4/mod/mod_macro.html) will help you. You'll just create one `...` and then add simple lines e.g. `Use SaasDomain company saas.example.net`, depending on the macro you have defined. As the domains will be custom, some manual work will always be needed. This is for minimizing that work. – Esa Jokinen Jun 22 '17 at 08:13
  • alright thanks a lot. So as per our converstation here are things we need:- 1)- Dedicated server or vps 2)- Virtual host 3)- And customers will point their domain name to our host. so i will give them Domain name servers. 4)- is there any cname involved? Can you correct above? – Naveen Jun 22 '17 at 08:16
  • That's a suitable procedure. My answer already has these two cases: don't use `CNAME` for `example.net`; you may use it for `saas.example.net`, but it's not necessary. Otherwise, I think my answer has all what is needed. The details may cause another question, later, and it is recommended to add distinct question if you have problems with the implementation. Until then, read the linked documentation and try to configure it by yourself – ask a question, when you have a delimited problem (and add what you have tried so far to solve it). Good luck! – Esa Jokinen Jun 22 '17 at 08:20
  • Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/60881/discussion-between-naveen-and-esa-jokinen). – Naveen Jun 22 '17 at 08:27
0

You can use RewriteRule's to redirect requests to a specific DocumentRoot and you can add the rule's dynamic to a .htaccess file.

It would be better to add virtualhosts to Apache's config files but then you have to restart Apache after every change.

.htaccess:

RewriteEngine On

RewriteCond %{HTTP_HOST} (clientone.saas.example.com)
RewriteRule ^(.*)$ /clientonefolder/$1 [R=301,L]

RewriteCond %{HTTP_HOST} (clienttwo.saas.example.com)
RewriteRule ^(.*)$ /clienttwofolder/$1 [R=301,L]
zarvox
  • 111
  • 1
  • 1
  • 5
  • What would add the new domain into `ServerAlias`, if the site isn't the default virtual host? Restarting Apache isn't necessary either; you can restart gracefully or reload. – Esa Jokinen Jun 20 '17 at 13:50