1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Creating subdomains using htaccess

Discussion in 'Apache' started by ojm, Aug 17, 2007.

  1. #1
    Hi,

    I wish to make folders (i.e. mysite.com/folder) into a subdomain (i.e. folder.mysite.com).

    I have found this:

    http://www.hm2k.com/posts/creating-subdomains-from-directories-using-mod_rewrite-in-apache-htaccess

    The idea was to have the ability to create unlimited subdomains simply by creating an appropreate directory for it in your html root directory.
    
    Since most people don’t have direct access to their httpd.conf, the obvious solution was to create a method using mod_rewrite within “.htaccess”. This also allowed it to be setup very easily and quickly.
    
    I have no immediate use for this solution, however I know it will come in very handy in the future.
    
    Someone I know was trying to figure this out earlier today, so I took it upon myself to figure out how to work out a solution for this problem.
    
    After much discussion with #apache @ EFnet, in particular TBF, we came about the following solution.
    
        #Grab the subdomain from the domain
        RewriteCond %{HTTP_HOST} ^([^.]+).hm2k.org$
        #Make sure the subdomain is not www or example
        RewriteCond %{1} !^(www|example)$
        #Check if the directory actually exists before we go there
        RewriteCond /home/hm2k/public_html/%1 -d
        #This stops it from looping
        RewriteCond %{REQUEST_FILENAME} !^/home/hm2k/public_html/
        #Finally, this is the actual rewrite
        RewriteRule (.*) /home/hm2k/public_html/%1/$1 [Last]
    
    Thanks to all those who helped, I hope this comes in useful to someone.
    Code (markup):
    But, it doesn't seem to be working.

    Anyone have any ideas on how to make this work, or another way to do it? Or maybe do I have to modify anything else?

    Thanks,
    Owen.
     
    ojm, Aug 17, 2007 IP
  2. bilal@revolutionhosting

    bilal@revolutionhosting Peon

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Make sure you modify the paths listed there ("/home/hm2k/public_html") into those appropriate for your site.
     
    bilal@revolutionhosting, Aug 17, 2007 IP
  3. ojm

    ojm Active Member

    Messages:
    156
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #3
    I've tried both:

      #Grab the subdomain from the domain
        RewriteCond %{HTTP_HOST} ^([^.]+).MYDOMAIN.com$
        #Make sure the subdomain is not www or example
        RewriteCond %{1} !^(www|example)$
        #Check if the directory actually exists before we go there
        RewriteCond /home/owen/public_html/%1 -d
        #This stops it from looping
        RewriteCond %{REQUEST_FILENAME} !^/home/owen/public_html/
        #Finally, this is the actual rewrite
        RewriteRule (.*) /home/owen/public_html/%1/$1 [Last]
    Code (markup):
    and

    #Grab the subdomain from the domain
        RewriteCond %{HTTP_HOST} ^([^.]+).MYDOMAIN.com$
        #Make sure the subdomain is not www or example
        RewriteCond %{1} !^(www|example)$
        #Check if the directory actually exists before we go there
        RewriteCond /public_html/%1 -d
        #This stops it from looping
        RewriteCond %{REQUEST_FILENAME} !^/public_html/
        #Finally, this is the actual rewrite
        RewriteRule (.*) /public_html/%1/$1 [Last]
    Code (markup):
    Tried that one cos its shared hosting so wasn't sure if I needed to do the home/owen... etc.

    And none of those worked.

    Any more ideas?
     
    ojm, Aug 17, 2007 IP
  4. Estevan

    Estevan Peon

    Messages:
    120
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    0
    #4
    enable wildcard dne in your server
     
    Estevan, Aug 18, 2007 IP
  5. ojm

    ojm Active Member

    Messages:
    156
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #5
    What do you mean by that Estevan?
     
    ojm, Aug 18, 2007 IP
  6. nick-a

    nick-a Active Member

    Messages:
    446
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #6
    nick-a, Aug 19, 2007 IP
  7. Estevan

    Estevan Peon

    Messages:
    120
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    0
    #7
    hello
    nick-a post a good link !

    to add this type nano /var/named/yoursite.com.db
    add
    * IN CNAME yourdomainhere.com
    save file restart named open /etc/httpd/conf/httpd.conf add in ServerAlias *.yourdomain.com yourdomain.com
    close restart httpd
    done you now create your subdomains
     
    Estevan, Aug 20, 2007 IP
  8. ojm

    ojm Active Member

    Messages:
    156
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #8
    Ok, well, I've added a *.mysite.com to the httpd.conf file, and also a *.mysite.com. as an A Record.

    Now, If I type in any site address, i.e. amsdfsdfadf.mysite.com or adsfjasdjfjadksfkjsjfadsjklfd.mysite.com, it goes to my websites homepage (mysite.com).

    How would I fix this up so that it would go to the directory?

    Any help would be appreciated, Owen.
     
    ojm, Aug 22, 2007 IP
  9. Estevan

    Estevan Peon

    Messages:
    120
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    0
    #9
    ojm
    now you need create a script or use htaccess !
    RewriteEngine On

    RewriteCond %{HTTP_HOST} !^www\.sitename\.com$
    RewriteCond %{HTTP_HOST} ^.+\.sitename\.com$
    RewriteCond %{REQUEST_URI} !index.php$
    RewriteRule ^(.*)$ %{HTTP_HOST}$1 [C]
    RewriteRule ^(.+)\.sitename\.com/.* /$1/index.php

    well if need more help please pm to me

    best
     
    Estevan, Aug 22, 2007 IP
  10. Mascka

    Mascka Peon

    Messages:
    49
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Mascka, Nov 5, 2008 IP