Dynamic subdomain system

9

Recently I saw an app / site called Sarahah, and an interesting feature was that when doing the registration, the username turned into a subdomain, something like "user1.site.com", is there any way to do this mod rewrite or any apache configuration without actually creating other subdomains?

    
asked by anonymous 14.09.2017 / 19:03

2 answers

3

Web Server

People do not realize this, but this is already set by default on your web server.

When you access a domain that was pointed to your server, but your server does not have any virtual host configured to receive that domain, it delivers the contents of the default web server directory. In most cases in /var/www/html .

All you have to do is set up your application within the web server's default directory and program it there to deliver a specific content based on the accessed domain. Be it done in PHP, NodeJS, Python or whatever. All of them have the resources to read the requested domain.

In PHP, for example, you can see which domain was used to get into your application via the global variable $_SERVER['HTTP_HOST'] .

But there is one however there. The DNS entries still need to be replied by the subdomain.

DNS entries

For this to really work without you having to create DNS entries on your domain for each user, you have to configure your domain to respond with wildcard .

Instead of creating a per-user DNS entry, you create a single entry that will account for anything. See below.

The normal would be (that we are accustomed to do):

(A|CNAME) -> usuario1.dominio.com.br -> serverIP
(A|CNAME) -> usuario2.dominio.com.br -> serverIP
(A|CNAME) -> usuario3.dominio.com.br -> serverIP
(A|CNAME) -> usuario4.dominio.com.br -> serverIP

An entry using wildcard would be:

(A|CNAME) -> *.dominio.com.br -> serverIP

With a wildcard entry configured and your application placed in the default directory of your web server, all you have to do now is what was said above: identify the domain being accessed through your application.

Not everything is flowers

Most DNS managers (the tool you use to make notes for your domain) do not support or allow the use of wildcards . CloudFlare is an example of those that do not allow this.

But all you have to do to resolve this is to find a DNS manager that allows you to point your domain to it. For registration purposes, the DigitalOcean DNS manager allows the use of wildcards .

    
14.09.2017 / 19:58
1

Good and simple solution via .htaccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.yourwebsite.com
RewriteCond %{HTTP_HOST} ([^.]+)\.yourwebsite.com
RewriteRule ^(.*)$ /path_to_your_site/httpdocs/work_out.php?url=$1

source

    
04.11.2017 / 21:39