htaccess: translating ANY subdomain into a directory

Discussion in 'Apache' started by James WP, Sep 17, 2007.

  1. #1
    Folks, you've got to help me - I am at my wit's end. I am an htaccess newb, and I've been through so many tutorials and forum topics but none of them seem to give a solution that works.

    Simply put, I want:

    sub.domain.com

    ...to redirect to:

    domain.com/sub

    ...where sub can be anything, not one of a predefined, hardcoded list of possibilities.

    I've tried all the following suggestions from various sources, and more, with no success:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^[www\.]*[a-z0-9-]\.domain\.com [NC]
    RewriteCond %{REQUEST_URL} !^/$1/.*
    RewriteRule ^(.*) /$1/$2 [L]
    Code (markup):
    RewriteEngine On
    
    Options +FollowSymLinks +Indexes
    RewriteEngine on
    RewriteBase /
    RewriteCond %{HTTP_HOST} !www\.domain\.com [NC]
    RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.domain\.com [NC]
    RewriteCond $1 !page\.php$
    RewriteRule (.*) /%2/page.php?username=$1 [L]
    Code (markup):
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^[a-zA-Z0-9\-\_]\.domain\.com$ [NC]
    RewriteRule ^(.*)$ http://domain.com/$1 [R=301, L]
    Code (markup):
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^([^.]+).domain.com$
    RewriteCond %{1} !^(www|example)$
    RewriteCond %{REQUEST_FILENAME} !^/public_html/
    RewriteRule (.*) /public_html/%1/$1 [Last]
    Code (markup):
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^[a-zA-Z0-9\-\_].domain.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www.[a-zA-Z0-9\-\_].domain.com$
    RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
    Code (markup):
    Some of these aren't even designed to do exactly what I want (like redirecting to a dynamic page rather than a directory), but I still can't get them to work. I'm sure each of them has its merits, but all I get is a "server not found" error in my browser.

    The only other line in my htaccess file is this, at the end:

    ErrorDocument 404 /404.php

    Could some kind soul please show me a working piece of code that I can cut and paste? This is starting to take years off my life. :(
     
    James WP, Sep 17, 2007 IP
  2. evera

    evera Peon

    Messages:
    283
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try these 2:

    RewriteEngine On 
    RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC] 
    RewriteRule ^(.*)$ http://domain.com/%1 [QSA,R=301,L]
    Code (markup):
    RewriteEngine on 
    RewriteCond %{HTTP_HOST} !^www\.domain\.com
    RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
    RewriteRule ^$ /%1 [L]
    Code (markup):
     
    evera, Sep 19, 2007 IP
  3. James WP

    James WP Active Member

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #3
    Thanks for your reply, evera. I've got round the problem a different way, which is probably not the best but I'm just happy that something finally works!

    By asking my host to enable wildcard subdomains, I've been able to catch errant URLs in the script of my index page and deal with them from there. Until then, requests for nonexistant subdomains hadn't even been reaching Apache, which is apparently why editing .htaccess was futile.
     
    James WP, Sep 20, 2007 IP
  4. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #4
    evera, they only catch the requests for those subdomains, it doesn't enable them.

    This can be done by using a server alias, or adding *.example.com in your control panel subdomain manager, or just asking your host nicely as you did :)
     
    krt, Sep 20, 2007 IP
  5. evera

    evera Peon

    Messages:
    283
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ah that was the problem, i just thought you had some syntax issue.
    But when it works its fine I guess.
     
    evera, Sep 21, 2007 IP
  6. colinrgodsey

    colinrgodsey Banned

    Messages:
    125
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #6
    yeah, you gotta keep in mind that subdomains have to resolve through dns before they even see your apache server
     
    colinrgodsey, Oct 1, 2007 IP