Subdomains are typically the portion of the URL that comes before the main part of the domain name. Dynamic Subdomains like tumblr.com and Google Blogger have been on the rise for a while with the introduction of .htaccess in PHP. These dynamic subdomains are the base of many cloud services prevalent out there.
Subdomains, we all are familiar with the needs of subdomains in today’s era. Subdomains help break the website into its smaller versions, which can help launch different career sites when required.
Dynamic subdomains serve the same purpose but with extended credibility of getting numerous subdomains under a single subdomain.
Let’s talk about the purposes which a subdomain can serve.
- Subdomains are commonly used to create different sections for a website which can be later utilized dynamically to host other career websites.
- It can help manage your domain DNS settings.
- Individual domains based on different languages can be created.
A subdomain is a look-alike to your Domain but much like an add-on to it. It is a separate part of any domain or website which operates under the main Domain. If your domain name is xyz.com, then you create a new subdomain with the name blog.xyz.com. Subdomains provide you with the freedom of creating a new website dependent on your domain name for a while. The existence of subdomains is there just because creating new websites from the old domains becomes easy. You don’t have to buy a new domain name for creating a new website.
On the other hand, Domain serves the purpose of your website to exist on the Internet. Without a domain name, your website won’t exist on the Internet.
Creating Subdomains:
We can create subdomains in two ways after the signup process is completed. Let’s talk about both the process later below,
1- DNS add-up.
Before you create a subdomain for your website, you will have to buy a domain name. Once you buy a domain name, you also get the subdomain rights. Whenever you create a subdomain, you need to follow the following steps,
- You need to enter your subdomain name in your DNS settings.
- Then you need to redirect to your server on which your subdomain is hosted.
When you are entering a record into your DNS settings, you will see that your URL, www.yoursite.com, is pointing towards yoursite.com, which makes “www” a subdomain too.
2- Using htaccess along with PHP
- Step 1: You need first to add your zone record in your DNS settings. Also, a zone record is a text file stored on the DNS server, which contains IP, name data, MX records, and other service records. A zone record can be an IpV6 host as well as a mail exchanger.
- Step 2: You need to create a custom record to serve all your subdomains. Now you have to select x record and then host * points to your IP address. For example(103.21.252.71)
- Step 3: Now, you have to add Cname(Canonical Record name, a type of source record name that maps one domain name to another). After you have added the Cname, you now have to host www points to your IP address.
- Step 4: After you have added all the record names, save all your DNS settings.
Working with Hosting Server
We can create a dynamic subdomain system with a .htaccess URL redirection configuration file. The files which will be needed are.
1- Root .htaccess
This file of code helps in redirecting http://www.examplewebsite.com to http://examplewebsite.com for the home page use.
Every subdomain redirects to the parent folder, though.
RewriteEngine On RewriteCond %{HTTP_HOST} ^www.yourwebsite.com RewriteRule (.*) http://yourwebsite.com/$1 [R=301,L] RewriteCond %{HTTP_HOST} ^yourwebsite\.com $ RewriteCond %{REQUEST_URI} !^/yourwebsite_folder/ RewriteRule (.*) /yourwebsite_folder/$1 RewriteCond %{HTTP_HOST} ^(^.*)\.yourwebsite.com RewriteCond %{REQUEST_URI} !^/yourwebsite_folder/ RewriteRule (.*) /yourwebsite_folder/$1
2- Inside .htaccess folder
This file is for rewriting the subdomain URLs.
From http://yourwebsite.com/index.php?siteName=toxic to http://toxic.yourwebsite.com
Options +FollowSymLinks RewriteEngine On RewriteBase / RewriteRule ^([aA-zZ])$ index.php?siteName=$1 RewriteCond %{HTTP_HOST} ^(^.*)\.yourwebsite.com RewriteRule (.*) index.php?siteName=%1
Index.php
This file contains executable statements with simple PHP code, using regular expressions validating the subdomain value.
<?php $siteName=''; if($_GET['siteName'] ) { $sitePostName=$_GET['siteName']; $siteNameCheck = preg_match('~^[A-Za-z0-9_]{3,20}$~i', $sitePostName); if($siteNameCheck) { //Do something. E.g., Connect the database and validate the siteName. } else { header("Location: http://yourwebsite.com/404.php"); } } ?> //HTML Code <!DOCTYPE html> <html> <head> <title>Project Title</title> </head> <body> <?php if($siteNameCheck) { ?> //Home Page <?php } else { ?> //Redirect to Subdomain Page. <?php } ?> </body> </html>
No Subdomain Folder
If you are using your home or root directory as your project directory, then you can use the following .htaccess file.
Options +FollowSymLinks RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^www.yourwebsite.com RewriteRule (.*) http://yourwebsite.com/$1 [R=301,L] RewriteRule ^([aA-zZ])$ index.php?siteName=$1 RewriteCond %{HTTP_HOST} ^(^.*)\.yourwebsite.com RewriteRule (.*) index.php?siteName=%1
These were some of the steps you can use when creating a subdomain. Creating a subdomain using htaccess always works efficiently, you just have to add some record names and then host it to your domain name with your IP address and your subdomain starts working flawlessly.
Leave a Reply