神刀安全网

Create Dynamic Subdomains using PHP and Htaccess

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.

Create Dynamic Subdomains using PHP and Htaccess

Create Dynamic Subdomains using PHP and Htaccess Create Dynamic Subdomains using PHP and Htaccess

Step 1

Go to your domain DNS(Domain Name Settings), click add zone record.

Create Dynamic Subdomains using PHP and Htaccess

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)

Create Dynamic Subdomains using PHP and Htaccess

Step 3

Repeat the same for HOST @ , here is the listed A records.

Create Dynamic Subdomains using PHP and Htaccess

Step 4

Now add CNAME record, HOST www POINTS TO @ this refers to your IP address.

Create Dynamic Subdomains using PHP and Htaccess

Step 5

CNAME list should be in following way.

Create Dynamic Subdomains using PHP and Htaccess

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 yourwesbite_folder

Create Dynamic Subdomains using PHP and Htaccess

(1) Root .htaccess

This file is redirection http://www.yourwesbite.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

(2) Inside Folder .htaccess

This file is rewriting the subdomain urls.

http://yourwesbite.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

More .htaccess tips: Htaccess File Tutorial and Tips.

index.php

This file contains simple PHP code, using regular expressions validating the subdomain value.

  //HTML Code    

//Home Page //Redirect to Subdomain Page.

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

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Create Dynamic Subdomains using PHP and Htaccess

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮