Dynamic subdomains like Google Blogger and Tumblr.com, I know most of the people are looking for better solution. Today I want to explain how to achieve this using .htaccess with PHP. I especially love to write .htaccess file, using this you can easily host multiple domains in single server. This dynamic subdomain system is the base for cloud services, soon I will come with new tutorial. I hope this post will solve your problem, please take a look this live demo.
Download Script Live Demo
Step 1
Go to your domain DNS(Domain Name Settings), click add zone record.
Step 2
You need to create a custom A record to serve all your subdomains. Select A record, HOST * POINTS TO: Your ID Address(Eg: 106.21.252.71)
Step 3
Repeat the same for HOST @, here is the listed A records.
Step 4
Now add CNAME record, HOST www POINTS TO @ this refers to your IP address.
Step 5
CNAME list should be in following way.
Step 6
Save all of your domain DNS settings.
Working with Hosting Server
We can achieve dynamic subdomains system with .htaccess URL redirection configuration file, here I have a project directory called yourwebsite_folder (1) Root .htaccess
This file is redirection http://www.yourwebsite.com to http://yourwebsite.com for home page use. All of the subdomain redirection to yourwebsite_folder
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
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 Folder .htaccess
This file is rewriting the subdomain urls.
http://yourwebsite.com/index.php?siteName=9lessons
to
http://9lessons.yourwebsite.com
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^([aA-zZ])$ index.php?siteName=$1
RewriteCond %{HTTP_HOST} ^(^.*)\.yourwebsite.com
RewriteRule (.*) index.php?siteName=%1
RewriteEngine On
RewriteBase /
RewriteRule ^([aA-zZ])$ index.php?siteName=$1
RewriteCond %{HTTP_HOST} ^(^.*)\.yourwebsite.com
RewriteRule (.*) index.php?siteName=%1
More .htaccess tips: Htaccess File Tutorial and Tips.
index.php
This file contains 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. Eg: Connect 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>
$siteName='';
if($_GET['siteName'] )
{
$sitePostName=$_GET['siteName'];
$siteNameCheck = preg_match('~^[A-Za-z0-9_]{3,20}$~i', $sitePostName);
if($siteNameCheck)
{
//Do something. Eg: Connect 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 root directory(htdocs/public_html) as a project directory, use this 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
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
Thanks for the great tip
ReplyDeleteThis is just amazing, is it any way we can create same in digital ocean
ReplyDeletecoOL dude!
ReplyDeletethanks..
Hi dude,
ReplyDeleteI want to this like redirection file.
for eg: wdcctest.com/index.php/WEWReafdfee-adfsd-sdfsdf
this like redirect in same index page. can you send that htaccess file code
Hello,
ReplyDeletePlease, did you mean we can use either of the methods above? Or, we still need to configure CNAME, A RECORD before .htaccess can work?
Thanks
You have to configure CNAME and A Record
DeleteOkay,
DeleteWhat happens to original (static) subdomain?
Thanks.
good lesson ;) thanks.
ReplyDeleteAwesome buddy.
ReplyDeleteunable to create for "Www".
ReplyDeleteI know the reason, but you can make it more pretty by adding a validation or popup message.
Thanks
Akshay Makadiya
Hello
ReplyDeletei have create the CNAME and A(Host) records but getting the : Internal Server Error
Can you help..
Great tip thanks This is just amazing.
ReplyDeleteNice article. Can you a post on dynamic subdomains with php and nginx.
ReplyDeletestill confuse can u tell more detail with video ?
ReplyDeletehi Mr thammada this is not workin for gramam.today domain in godDDY
ReplyDeletei use this for creat user profile rediect to subdomain but profile is my path not auto redirect to sub domain its work when direct open how i do this help me plz
ReplyDeletevery nice sir great
ReplyDeletehello, I was unable to create a custom A record for HOST @. it gives me an error message saying "That is not a valid zone name"
ReplyDeleteSame for me too..I tried to create CNAME for host@ and www. But it shows me error.
DeleteI also couldn't add cname record, It shows me error, i m having my domain at bigrock
DeleteMe too =(
Deleteam trying to implement the dynamic subdomain on godaddy but it is not working. Example: signup.mysite.com. the signup is a folder in my root directory. Help from someone...
ReplyDeleteHi, i followed your steps and done all those things. But i got "pageok" text with blank page. My hosting domain and server in GODADDY. Please help me. Thanks.
ReplyDeleteCheck your server .htaccess is working or not?
DeleteHi, thanks for your response. In my server .htaccess working perfectly. But we are having multi domain shared host in godaddy. We don't have dedicated IP. All domains having same IP address. So my doubt, wildcard DNS(*.mywebsite.com) not working in shared host(without dedicated IP). Is it right?
DeleteNice, got to try n feedback..
ReplyDeleteCan i need VPS/Dedicated ? or available on cPanel Reseller ??
ReplyDeletecan you send me example code ...
ReplyDeleteNice article sir. is this SEO friendly?
ReplyDeletehello sir,
ReplyDeleteFirst of all thanks for this great tutorial but i am not able to do the same please help me regarding this .
Please give me your personal mail or anything so that i can send everything to you .
Please help me i am in big trouble
Thanks
subdomain folder is not creating
ReplyDeleteHi Dear,
ReplyDeletei have create the CNAME and A(Host) records but getting the : Internal Server Error
Can you help..
thanks dude
ReplyDeletehello,
ReplyDeleteThank for wonderfull script, I am using it in my new blog site to display seperatily user's blog. Here i create subdomain using user's username like http://username.sitename[.]com.
My issue is,
when i go on user's blog there session is not working.
Second is Access-Control-Allow-Origin, fonts not working .
Thanks
Hi,
ReplyDeleteYour article is very interesting. I have a question. How would you maintain a wildcard sub domain SSL cert on YOURWEBSITE.COM in this scenario?
Thank you.
I tried this and is working fine. But how to exculde css js and other folder from this redirection, else all my css and js files are redirected to index.php
ReplyDeleteHello, this is a great article but, i have to make functionality like that, please guide us is it possible on shared hosting.? or need dedicated server,
ReplyDeleteThank you
Hello i have a Question will i be able to Delete a Sub domain later down the road? Second question is it possible to create this with user accounts. So not just anyone can make one?
ReplyDeletepage not found when i create page fill and create. Its relate about dns setting or a Programmically error please send back answer fast its creatically important. :)
ReplyDeleteServer Go daddy.
could I use htaccess in xampp application ?
ReplyDeleteAs in your example (http://page1.thewallchat.com/), CSS files and others do not load, everything in the internal folder is redirected to index.php. How to solve this?
ReplyDeleteFix the base URL for CSS and Javascript sources
DeleteThis comment has been removed by the author.
ReplyDeleteHi, I was unable to create a custom A record for HOST @. it gives me an error message saying "That is not a valid zone name". I am using godaddy shared hosting. Please help me.
ReplyDeleteThanks.
Hello, Do I have to set up ServerAlias in apache config for wildcard subdomains?
ReplyDeleteIs it possible to point sub domain to sub directory.
ReplyDeleteHello i have a question i was wondering if it is possible to create this using local hosting like xampp or something like this?
ReplyDeleteGo to your domain DNS(Domain Name Settings), click add zone record ==> go my domain DNS but no view (ADD zone) || DNS setting in goddady ==> We can't display your DNS information because your nameservers aren't managed by us.|| in this case EDIT my Advanced Features (1.Manage Templates,2.DNSSEC,3.Host names) ????
ReplyDeleteDoes it work on shared hosting?
ReplyDeletehow does it works on xampp ?
ReplyDeleteProblems with url Access-Control-Allow
ReplyDeleteAwesome! I have been searching for help on this. thanks dude you just made my day.
ReplyDeleteIs this work in localhost?
ReplyDeletePlease how do I use two variables like demo.example.com/product?id=cloth
ReplyDeleteusing the first Get variable as subdomain and the second being id as parameter?
Thanks for sharing this. How can we have https for the subdomain. wildcard ssl is taking time to start working as https
ReplyDeleteThis is very interesting.
ReplyDeleteI have one issue.
How can I SSL active for all Dynamic Subdomains?
anybody explain if automatic subdomain on member sign up could be done on shared hosting?
ReplyDelete