Wildcard DNS - Wildcard Hosting
Consider your hosting company allows you to use only 3 sub domains.
But as your website grows, you might need more number of sub domains. What you will do?
Or
Consider you want to set up https for all your sub domains.
For each sub domain you will have to buy an SSL certificates. This is costly. In such cases, you can use wildcard subdomains.
Create a wildcard sub domain. Buy 1 SSL certificate and use it for all your sub domains. It is also refer this as Wildcard hosting.
** Note **:
As a regular user, you will not be able to accomplish this task.
Contact your website hosting company for support.
The following steps are involved in Wildcard Hosting:
1. Adding the DNS entries.
2. Setting up the Apache entries.
3. Redirecting sub domain to the correct folder.
Wildcard DNS and its Implementation
Adding the DNS entries
If you have privilege to edit the DNS zone, you will have to add the following entries.
Example:
*.domain-name.com. IN A 121.0.0.1
Once the DNS propagation gets completed. You can try digging any sub domains.
Example:
test.domain-name.com
abc.domain-name.com
123.domain-name.com
All the above sub domain will point to the server.
Setting up the Apache entries
Now all the sub domains are pointing to the server. But there is no virtual
host entry for it. So you will get the Apache page.
You just need to edit the virtual host entries for that particular domain. Add a ServerAlias " *.domain-name.com ".
Example:
<VirtualHost IP:443>
ServerName domain-name.com
ServerAlias *.domain-name.com
DocumentRoot /home/user/public_html/
....................
.............
</VirtualHost>
Then restart Apache.
Now accessing any sub domains will give up the main website page.
This is not what you are looking for. You want the sub domains to point to its respective folder.
Example:
Accessing " test.domain-name.com ", should point to the test folder under " public_html " directory.
To get this done, we can use Rewrite conditions.
Redirecting sub domain to the correct folder
To make each sub domain redirect to its correct folder, you can use the
rewrite rules.
Open the .htaccess file under " public_html " and add the below given entries.
Example:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.com [NC]
RewriteRule ^(.*)$ https://www.domain.com/%1 [L,R]
Related posts:
Your IP address1 is: 172.18.0.2
Tutorials
Latest Updates
Follow us
- Tech-Tutorials twitter.com/techcuriosity