/var/net/sys/admin/blog
| More

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 “freelinuxtutorials.com” {
type master;
file “/var/named/freelinuxtutorials.com.db”;
};

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

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

freelinuxtutorials.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 freelinuxtutorials.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 freelinuxtutorials.com
ServerAlias *.freelinuxtutorials.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.freelinuxtutorials.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.freelinuxtutorials.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.

27 Responses to “Automatic unlimited subdomains via Apache mod_rewrite”

  1. Akbari Divya

    on August 14 2010

    Thanks for helping me.
    This is working

  2. admin

    on August 25 2010

    @akbari, no problem. thanks for dropping by

  3. Mike Priest

    on June 1 2011

    Great post, well detailed.

    Do you have a link to the DNS configutation?
    Im having trouble trying to setup mydomain.com -> 127.0.0.1 using localhost environment.

    Im using Ubuntu in VMWare.

  4. Noel

    on January 24 2012

    Very helpful information, thank you – I managed to get Apache configured correctly in a matter of minutes based on your advice. Our hosting provider allows us to edit our server’s DNS record directly (using their own control panel) so this can also save some time.

  5. Thomas R.

    on February 9 2012

    Hello,

    great solution!

    But I have one small problem with this: On my server doesn`t work if the name of subdomain is same as folder in root directory of unix, for example dev.domain.xx etc.domain.xx etc.
    I don`t know why, can you help?

    Thanks

    Thomas

  6. admin

    on February 10 2012

    Hi Thomas, thanks for dropping by. Bind DNS should work on any flavors of unix/linux. Can post the error you are getting if there’s any or can pls. elaborate more details. thanks.

  7. admin

    on February 10 2012

    @Noel, no problem, my pleasure. Thanks.

  8. Https://ferrann.com/blog/shampoo-matizador/

    on October 24 2018

    https://ferrann.com/blog/shampoo-matizador/

    automatic unlimited subdomains via Apache mod_rewrite | Free Linux Tutorials

  9. shampoo sin sal

    on October 24 2018

    shampoo sin sal

    automatic unlimited subdomains via Apache mod_rewrite | Free Linux Tutorials

  10. Tintes de cabello combinados

    on October 24 2018

    tinte

    automatic unlimited subdomains via Apache mod_rewrite | Free Linux Tutorials

  11. https://dailyuploads.net/1u57l2jv3xqk

    on October 27 2018

    If some one desires expert view about blogging afterward i advise
    him/her to go to see this web site, Keep up the nice
    work.

  12. Exani ll

    on October 28 2018

    https://www.mortaji.co/simulador-exani-ii/

    automatic unlimited subdomains via Apache mod_rewrite | Free Linux Tutorials

  13. Shampoo sin sulfato

    on October 29 2018

    ferrann.mx

    automatic unlimited subdomains via Apache mod_rewrite | Free Linux Tutorials

  14. Decolorante para pelo

    on November 7 2018

    ferrann.com

    automatic unlimited subdomains via Apache mod_rewrite | Free Linux Tutorials

  15. Kerastase

    on November 9 2018

    ferrann.mx

    automatic unlimited subdomains via Apache mod_rewrite | Free Linux Tutorials

  16. Https://creativo.studio/copywriter-creacion-de-contenidos/

    on November 16 2018

    https://creativo.studio/copywriter-creacion-de-contenidos/

    automatic unlimited subdomains via Apache mod_rewrite | Free Linux Tutorials

  17. Charles Pellegrino

    on November 23 2018

    It’s wonderful that you are getting ideas from this post as well as from our dialogue made here.

  18. Jermaine McGaw

    on November 29 2018

    Normally I do not learn article on blogs, but I would like to say that this write-up very forced me to take a look at and do so! Your writing taste has been surprised me. Thank you, very nice article.

  19. Shella Veitch

    on November 29 2018

    I appreciate, lead to I found just what I used to be looking for. You have ended my four day lengthy hunt! God Bless you man. Have a great day. Bye

  20. Brett Pung

    on December 2 2018

    If you have young children, be sure you secure any area where you have these hazardous materials stored, i. Unfortunately, many people only become hyper-aware of their bodies when something goes wrong. Instead of drugs that just treat the symptoms such as the resolve the complexities, we empower you to deal with your symptoms naturally in the end take care in the basic reasons behind your pain.

  21. Adriene Bolduc

    on December 5 2018

    You can also prevent clogging with the arteries and helps inside the functioning in the circulatory system not merely, but the nervous system, digestive and respiratory systems. Exercise naturally is incredibly beneficial but doesn’t directly lower cholesterol levels, instead it is going to strengthen one’s heart and entire circulatory system. We want to utilize right dieting and exercise program to cut back glycogen.

  22. Otto Gorsuch

    on December 6 2018

    This version also provides longer battery life as compared with its predecessor; hence you will possess four (4) era of nonstop reading time prior to you will find the are looking for an outlet. They think it is quite comfortable of going about the thought of dating over the internet, through the vast span of time they arrive at spend. Products are added and subtracted every so often while they become available.

  23. Anita Ericson

    on December 10 2018

    با پیشرفت علوم و تکنولوژی نیاز به تمرکز بر روی آیتم های خاص ساخت و ساز ، بویژه آیتم های صنعی امر ی اجتناب ناپذیر قلمداد گردید و به موازات با توجه به سال ها کسب تجربیات فراوان در فعالیت های مستقیم و غیر مستقیم با شرکت های به نام مشاور و پیمانکار در زمینه فنی و اجرایی، اعضای شرکت تصمیم گرفتند تا با انتخاب سقف های مرکب عرشه فولادی بشکل تخصصی فعالیت خود را بر روی این آیتم متمرکز کنند.

  24. Francesco Maxie

    on December 15 2018

    The social media marketing page can also be extremely important since it not only helps the individuals who question and the brand but in addition the people that look at the page. For this reason, you can even avail the assistance of a nail professional to accomplish the service as an alternative to attempting to use the process yourself at home. Some people feel that they must let hairs grow out so long as possible before coming to their laser removal appointment but this is a myth.

  25. Dallas Salkauskas

    on January 5 2019

    Because the admin of this site is working, no doubt very rapidly it will be renowned, due to its quality contents.

  26. Drusilla Worrell

    on October 30 2019

    Thanks for sharing your thoughts. I really appreciate your efforts and I am waiting for your further post thank you once again.

  27. Leanne Bowens

    on November 29 2019

    Unquestionably imagine that that you said. Your favourite reason seemed to be on the web the simplest thing to remember of. I say to you, I definitely get irked at the same time as other people think about issues that they plainly do not recognise about. You controlled to hit the nail upon the top and outlined out the whole thing with no need side effect , other folks could take a signal. Will probably be back to get more. Thank you

Comment RSS · TrackBack URI

Leave a comment

Name: (Required)

E-mail: (Required)

Website:

Comment:

 

About FLT

This site is dedicated to everyone who likes to learn and explore the beautiful world of Linux. If you have comments and suggestions, please feel free to email at freelinuxtutorials@gmail.com. I am happy to serve and share things esp. that is free and enjoyable as Linux.