www.webdeveloper.com
Results 1 to 3 of 3

Thread: Creating Pseudo Subdomains in Apache

  1. #1
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    700

    Creating Pseudo Subdomains in Apache

    Hi All,

    I'm very new to Apache, so please forgive me if the jargon isn't right.

    I have a Debian based Apache 2 server, and I want to create pseudo subdomains. As I understand it, this is achieved through an entry in one of the Apache conf files.

    For example, lets say we have the domain 'domain.tld'. I want to create 'sub.domain.tld'. The 'real' way to do it takes time to become active, and seems overly complex (even though the control panel I'm using creates it all for me), AND isn't accessible as http://www.sub.domain.tld, only as http://sub.domain.tld.

    I read a reference somewhere on the web that talked about 'pseudo' subdomains, which are just Apache redirects to the content of directories within domain root directory (the html folder). Unfortunately, it didn't detail how to do that.

    What I'm looking for is a single entry in an Apache .conf file that reads the request 'sub.domain.tld' and simply redirects to domain.tld/sub (while leaving the displayed address as 'sub.domain.tld'). If /sub doesn't exists, an error is generated. As soon as /sub is created, the redirect comes into effect and sub.domain.tld works.

    Is this possible?
    If so, how do I get it done?

    Thanks
    CTB

  2. #2
    Join Date
    Jul 2006
    Location
    Nebraska
    Posts
    380
    If you can get at your vhosts, add a new one and change it up with some php love.

    Code:
    <VirtualHost *:80>
            ServerAdmin guy@example.tld
            ServerName sub.domain.tld
            DocumentRoot /path/to/real/web
            .... Other Directives.....
    </VirtualHost>
    <VirtualHost *:80>
            ServerAdmin guy@example.tld
            ServerName www.sub.domain.tld
            DocumentRoot /path/to/special/folder
            .... Other Directives.....
    </VirtualHost>
    Then in that special folder, put one item, a PHP file with this sort of thing going on:
    PHP Code:
    <?php
      header
    ('Location: http://sub.domain.tld/');
    ?>
    You might want to consider adding directives for all the server error pages in the www.sub domain to redirect to that index, so no matter what it'll get there.

    Probably not the best solution, but maybe it'll hold you over.

  3. #3
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    700
    Thanks Pirate (or should that just be' aaarrrrgg'),

    I've been doing some diggin on this, and was amazed at how simple the solution actually is.

    Assuming the A record in the zone configuration is set up to take wildcards, all I needed to do is add this to vhosts.conf...

    ServerAlias domain.tld *.domain.tld

    And now any.domain.tld ends up at domain.tld, where that "php love" can weave its magic. And because Apache simply starts at the top and works its way down until it finds a match, if there are real sub.domains, as long as their <VirtualHost> records come before this one, Apache will handle them.

    I love the simplicity of it.

    Thansk for your help.
    Cheers CTB

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles