• Home
  • PHP
  • MySQL
  • Demos
  • HTML
  • CSS
  • jQuery
  • Framework
  • Social
  • Request Tutorial
PHP Lift
  • Home
  • Demos
  • Advertisement
PHP Lift
  • Home
  • PHP
  • MySQL
  • Demos
  • HTML
  • CSS
  • jQuery
  • Framework
  • Social
  • Request Tutorial
  • Follow
    • Facebook
    • Twitter
    • Google+
    • Pinterest
    • Youtube
    • Instagram
    • RSS
How do you create subdomains dynamically using PHP when a user signs up?
Home
PHP

How do you create subdomains dynamically using PHP when a user signs up?

April 5th, 2021 Rick Boklage PHP 0 comments

Facebook Twitter Google+ LinkedIn Pinterest

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.

  1. Subdomains are commonly used to create different sections for a website which can be later utilized dynamically to host other career websites.
  2. It can help manage your domain DNS settings.
  3. 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.

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)

Related

  • Tags
  • htaccess
  • PHP
Facebook Twitter Google+ LinkedIn Pinterest
Next article How do I send an auto-reply message on Skype?
Previous article 7 Top Reasons to Learn JavaScript in 2021

Rick Boklage

Rick Boklage is working for Focus It Solution. Focus It Solution helps businesses embrace technology and offers custom web-application services . Our developers create robust and reliable web applications for diverse industries such as healthcare, insurance, banking, manufacturing, and many more. Get in touch with him now for any assistance regarding Web Development.

Related Posts

How to Extract Text from PDF using PHP PHP
May 17th, 2022

How to Extract Text from PDF using PHP

Is PHP dead in 2021? Is PHP still relevant or worth the effort? PHP
April 3rd, 2022

Is PHP dead in 2021? Is PHP still relevant or worth the effort?

Simple PHP REST API with Slim, PHP & MySQL API
March 3rd, 2022

Simple PHP REST API with Slim, PHP & MySQL

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

    Advertisement
    Like us
    Recent Posts
    • How to Extract Text from PDF using PHP
    • 3 reasons why you SHOULD write tests
    • How to create a screen recorder in JavaScript
    • Best 10 Programming Languages that will rule in 2022
    • Top 7 Websites To Get Your First Paid Internship
    Categories
    • API
    • Bootstrap
    • Bot
    • CSS
    • CSS 3
    • Database
    • Designing
    • Framework
    • Guide
    • HTML
    • HTML 5
    • JavaScript
    • jQuery
    • MySQL
    • Node.js
    • oAuth
    • Payment
    • PHP
    • Python
    • Social
    • Tips
    • WordPress
    Weekly Tags
    • PHP
    • How to
    • javascript
    • api
    • MYSQL
    • jQuery
    • PHP Basics
    • Programming Habits
    • HTML5
    • PHP framework
    • About
    • Privacy Policy
    • Back to top
    © PHPLift 2021. All rights reserved.