February 2020
M T W T F S S
« Jan    
 12
3456789
10111213141516
17181920212223
242526272829  

Categories

WordPress Quotes

What we plant in the soil of contemplation, we shall reap in the harvest of action.
Meister Eckhart
February 2020
M T W T F S S
« Jan    
 12
3456789
10111213141516
17181920212223
242526272829  

Short Cuts

2012 SERVER (64)
2016 windows (9)
AIX (13)
Amazon (40)
Ansibile (19)
Apache (135)
Asterisk (2)
cassandra (2)
Centos (211)
Centos RHEL 7 (268)
chef (3)
cloud (2)
cluster (3)
Coherence (1)
DB2 (5)
DISK (25)
DNS (9)
Docker (30)
Eassy (11)
ELKS (1)
EXCHANGE (3)
Fedora (6)
ftp (5)
GIT (3)
GOD (2)
Grub (1)
Hacking (10)
Hadoop (6)
health (2)
horoscope (23)
Hyper-V (10)
IIS (15)
IPTABLES (15)
JAVA (7)
JBOSS (32)
jenkins (1)
Kubernetes (7)
Ldap (5)
Linux (188)
Linux Commands (166)
Load balancer (5)
mariadb (14)
Mongodb (4)
MQ Server (24)
MYSQL (84)
Nagios (5)
NaturalOil (13)
Nginx (35)
Ngix (1)
openldap (1)
Openstack (6)
Oracle (35)
Perl (3)
Postfix (19)
Postgresql (1)
PowerShell (2)
Python (3)
qmail (36)
Redis (12)
RHCE (28)
SCALEIO (1)
Security on Centos (29)
SFTP (1)
Shell (64)
Solaris (58)
Sql Server 2012 (4)
squid (3)
SSH (10)
SSL (14)
Storage (1)
swap (3)
TIPS on Linux (28)
tomcat (62)
Ubuntu (1)
Uncategorized (30)
Veritas (2)
vfabric (1)
VMware (28)
Weblogic (38)
Websphere (71)
Windows (19)
Windows Software (2)
wordpress (1)
ZIMBRA (17)

WP Cumulus Flash tag cloud by Roy Tanck requires Flash Player 9 or better.

Who's Online

27 visitors online now
10 guests, 17 bots, 0 members

Hit Counter provided by dental implants orange county

Automatic unlimited subdomains via Apache mod_rewrite

Wondering how blogger website gives you a subdomain from the blogspot.com domain based on your username of your choice? Well, the concept for this tutorial is somewhat related with it on setting up unlimited subdomains. I’m not sure how do they do it because it could be done by any script, hardcoded or not, thru static or dynamic way via database, many possibilities, and one possible way is thru URL rewriting via Apache’s mod_rewrite.

Requirements of this setup:

a. Apache 2.x or later
mod_rewrite module should be enable. To check if it’s enabled you should see this line on your httpd.conf

LoadModule rewrite_module modules/mod_rewrite.so

b. BIND 9.x or later
-Setting up the DNS Server is not part of this tutorial, it will be discussed on another topic.

Anyway to create automatic subdomains, these are the steps.

1. Configure your DNS server to use the wildcard (*) and points to your IP address.
Assuming you already setup the zone of your domain on named.conf , something like:

—@named.conf—

zone “rmohan.com” {
type master;
file “/var/named/rmohan.com.db”;
};

—@/var/named/rmohan.com.db—

$TTL 86400
@ IN SOA rmohan.com. root.rmohan.com. (
; dmn [flt.com] timestamp entry BEGIN.
2008092901
; dmn [flt.com] timestamp entry END.
8H
2H
4W
1D )
IN NS ns.rmohan.com.
IN NS ns2.rmohan.com.

rmohan.com. A 192.168.1.100
ns IN A 192.168.1.80
ns2 IN A 192.168.2.80
* IN A 192.168.1.100
; sub [{SUB_NAME}] entry BEGIN.
; sub [{SUB_NAME}] entry END.

As you can see above, 192.168.1.100 is pointed to *. Meaning if you ping any subdomains (existing or non-existing) of rmohan.com, it will all points to the same IP address

2. Configure your Apache
-To not really messed up your existing httpd config, is it better to create a new config and include it. Something like

@httpd.conf

Include conf.d/flt/*.conf

Create your virtualhost for your domain and add this rewrite rules on it.

#vi /etc/httpd/conf.d/flt/flt.conf

<VirtualHost 192.168.1.100:80>
DocumentRoot “/var/www/html/flt/subdomains”
ServerName rmohan.com
ServerAlias *.rmohan.com
<Directory /var/www/html/flt/subdomains>
Options Includes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>

#Rewrite Starts*
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www|m|secure|admin).* [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.freelinuxtutorials\.com
RewriteCond /var/www/html/flt/subdomains/%1 -d
RewriteRule ^(.*) /%1/$1 [L]

</VirtualHost>

*Explanation (I’ll try to explain it as easy as I can)

1.RewriteEngine on = tells to enable Rewrite, off to disable it
2. It means only matches URLs that don’t start with www,m,secure or admin subdomains. This is important so it will not redirect to a particular directory e.g. m.rmohan.com to /var/www/html/flt/subdomains/m/
3. ^([^\.]+)\. ==> these are combination of regular expression syntax of mod_rewrite, meaning
^ – start of syntax
. any single character
(…) group section
+ is added to make sure there is at least one char that matches
To sum up, it denotes anything that does not have a period, example is “subdomain”
4.the -d is added to checked if that directory does not actually exist
5. ^(.*) /%1/$1 ==> it will take the requested path and put into a backreference, rewrite it to point to that directory

%1 = subdomain
$1 = path backreference, based on the Rewrite condition above
L = Last Rule

3. Restart or Reload Apache service (depends on your distro)
something like
#service httpd restart
or #/etc/init.d/apache2 restart

4. Testing
Create a subdirectory named “darwin” under /var/www/html/flt/subdomains

#mkdir -p /var/www/html/flt/subdomains/darwin
#cd /var/www/html/flt/subdomains/darwin

Create a sample php file
#vi index.php

<?php
echo $_SERVER[‘SCRIPT_FILENAME’];
?>

Save and Exit

Access on your browser http://darwin.rmohan.com, this should appear:

/var/www/html/flt/subdomains/darwin/index.php

That’s it. The subfolder name created on that directory will be your subdomain and will point it to that directory path.

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  

  

  

Blue Captcha Image
Refresh

*

Protected by WP Anti Spam