Results 1 to 12 of 12
  1. #1
    Join Date
    Dec 2002
    Location
    chica go go
    Posts
    11,876

    Mikey's Instant Subdomains

    This tutorial will guide you on the steps needed to make subdomain creation as easy as creating a single directory.

    Step One
    Add a CNAME record for your domain for *.yoursite.com to your actual domain.

    *.yoursite.com. 14400 IN CNAME yoursite.com.

    make sure to include appropriate periods in those addresses.

    Step Two
    Open up your httpd.conf and add a host into your ServerAlias directive:

    ServerAlias *.yoursite.com yoursite.com

    Step Three

    navigate to your site's public_html directory, and add this code to your .htaccess, if the .htaccess file doesn't exist, create one by typing "vi .htaccess", you can learn more about using vi at This Thread

    RewriteCond %{HTTP_HOST} ^[^.]+\.yoursite\.com$
    RewriteRule ^(.+) %{HTTP_HOST}$1
    RewriteRule ^([^.]+)\.yoursite\.com(.*) /home/yoursite/public_html/$1


    Now, you can simply create a directory inside your public_html directory, and the subdomain will be instantly available.
    Last edited by SoftWareRevue; 05-25-2005 at 02:49 PM.

  2. #2
    Join Date
    Mar 2004
    Location
    Arizona
    Posts
    94
    thanks mikey - that could come in handy in the future. Glad you figured that thing out - I'm so not the cgi/apache coder....

  3. #3
    Join Date
    Dec 2004
    Location
    Canada
    Posts
    1,097
    Don't forget mod_vhost_alias .

    Good stuff.

  4. #4
    Join Date
    Dec 2004
    Location
    Behind You.
    Posts
    709
    apologizes in advance for thread digging, but this is the only place I could find this code.

    Ok so I did everything, but I guess there is something wrong in my .htaccess file, because no matter what I put in there, I see the MAIN www.mydomain.com website.


    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^[^.]+\.mydomain\.org$
    RewriteRule ^(.+) %{HTTP_HOST}$1
    RewriteRule ^([^.]+)\.mydomain\.org(.*) /home/mydomain/public_html/$1


    What did I get wrong? I'd really like to get this up and running, I plan to have

    ANYSUBDOMAIN.mydomain.com
    - read from
    mydomain.com/~ANYSUBDOMAIN

    Any help I can get is appreciated? I added the appropriate things in the /var/named/mydomain.com.db and in the httpd.conf file.

    What can I do to get this mod rewrite running?

    Thanks.

  5. #5
    Join Date
    Jan 2005
    Location
    Scotland, UK
    Posts
    2,681
    Do you have AllowOverRide set to none in httpd.conf? if so set it to all.
    Server Management - AdminGeekZ.com
    Infrastructure Management, Web Application Performance, mySQL DBA. System Automation.
    WordPress/Magento Performance, Apache to Nginx Conversion, Varnish Implimentation, DDoS Protection, Custom Nginx Modules
    Check our wordpress varnish plugin. Contact us for quote: sales@admingeekz.com

  6. #6
    Join Date
    Dec 2004
    Location
    Behind You.
    Posts
    709
    <Directory />
    Options All
    AllowOverride All
    </Directory>

    That is what is set. What other bright ideas?

  7. #7
    Join Date
    Dec 2004
    Location
    Behind You.
    Posts
    709
    can someone replace mydomain and org with their domain and tld and test it and let me know if it even works or not?

    I keep getting either 404 or 500 errors when i'm messing around with it.

    I KNOW wildcarded domains is on for a fact, so the problem is not with that, because

    ANYTHING.MYDOMAIN.ORG goes to www.mydomain.org

  8. #8
    Join Date
    Dec 2004
    Location
    Behind You.
    Posts
    709
    RewriteEngine on
    RewriteBase /

    # rewrite <subdomain>.example.com/<path> to example.com/<subdomain>/<path>
    #
    # Skip rewrite if no hostname or if subdomain is www
    rewriteCond %{HTTP_HOST} .
    rewriteCond %{HTTP_HOST}!^www\. [NC]
    # Extract (required) subdomain (%1), and first path element (%3), discard port number if present (%2)
    rewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^([^.]+)\.zaxy\.org(:80)?<>/([^/]*) [NC]
    # rewrite only when subdomain not equal to first path element (prevents mod_rewrite recursion)
    rewriteCond %1<>%3!^(.*)<>\1$ [NC]
    # rewrite to /subdomain/path
    rewriteRule ^(.*) /%1/$1 [L]

    I tried that code above, and I get 500 error.

    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com [NC]
    RewriteRule ^(.*) http://www.yourdomain.com/$1 [L,R=301]

    I tried that code, and I believe this one worked, but it redirected the browser to the folder location, and I don't want that. I want anysubdomain.mydomain.com to READ from mydomain.com/subdomain/ , not redirect. I could do that with a simple normal 301. I tried replacing the

    RewriteRule ^(.*) http://www.yourdomain.com/$1 [L,R=301]
    with
    RewriteRule ^(.*) /home/mydomain/public_html/$1/ but it didn't do any good. Still 500 error.

  9. #9
    Join Date
    Aug 2005
    Location
    Canada
    Posts
    862
    The backref in REGEX "\1" doesn't work in recent version of Apache.
    rewriteCond %1<>%3!^(.*)<>\1$ [NC]

    It seems this trick worked on older Apache, but not anymore, AFAIK.

    Also, this code is useless.
    rewriteCond %{HTTP_HOST} .

    You can't do what you want in the .htaccess because of infinit looping.
    You should put similar code in httpd.conf.

    Other than per-dir context (.htaccess and <Directory>),
    there is no problem of unwanted looping.
    ([L] works as expected.)

    Maybe I'll write a patch to solve this, one day.

  10. #10
    Join Date
    Aug 2005
    Location
    Canada
    Posts
    862
    I found another way to stop infinite loop (not ideal, though...).

    Code can be as simple as this:
    RewriteEngine on
    RewriteBase /
    rewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com [NC]
    rewriteRule ^(.*)$ %2/$1 [L]

    But in each one of subdirectory for the subdomain,
    there must be a .htaccess file that contains;
    RewriteEngine Off
    or even;
    RewriteEngine On

    These have teh effect of stopping the processing.
    If the .htaccess is deleted or the code is removed,
    it will create 500 error with max redirect (Apache1.3.28 and above)
    or Apache going crazy (below 1.3.28).

    Having said that, better method is using mod_vhost_alias,
    or doing similar redirect in other than per-dir context (usually in virtualhost).
    In case it msut be done in .htaccess, using a prefix to the directory names will solve the problem.
    (The prefix can be anything that doesn't occur in other directories or files, like "S_", "_", "USER-","sub/" and so on)

    RewriteEngine on
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com [NC]
    RewriteRule !^/*prefix_ prefix_%2%{REQUEST_URI} [L]

  11. #11
    Hello,
    I'm sorry for diging this thread out, but is there a way to do this in Apache 1.3.3. ?

    I'm having trouble with this code:

    Code:
    Step One
    Add a CNAME record for your domain for *.yoursite.com to your actual domain.
    
    *.yoursite.com.       14400   IN      CNAME   yoursite.com.
    
    make sure to include appropriate periods in those addresses.
    
    Step Two
    Open up your httpd.conf and add a host into your ServerAlias directive:
    
    ServerAlias *.yoursite.com yoursite.com
    
    Step Three
    
    navigate to your site's public_html directory, and add this code to your .htaccess, if the .htaccess file doesn't exist, create one by typing "vi .htaccess", you can learn more about usin....
    
    RewriteCond %{HTTP_HOST} ^[^.]+\.yoursite\.com$
    RewriteRule ^(.+) %{HTTP_HOST}$1 
    RewriteRule ^([^.]+)\.yoursite\.com(.*) /home/yoursite/public_html/$1
    Can anyone PLEASE help me...?

    I have read the whole thread, but I can't find solution, and this is ONLY place on the web where I was able to find it.

    Thank you.

  12. #12
    Hello... It's me again.. .Sorry for not understanding the tread, but I have solved my problem...

    Sorry again....

Posting Permissions

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