Directory Mapping in htaccess for Subdomains

Posted on 2006-03-30
Last Modified: 2008-01-09
I am hosting multiple websites with a company whose directory structure is thus... Primary Domain resides in public_html (which is where the htaccess file resides), And additional domains are subdirectories of this domain. I want to create a directory in public_html which is accessable by all the additional domains. As this is a shared sever I don't have access to the .conf file so it must be done via htaccess. Thanks for you help.
Question by:easttncs
    LVL 27

    Accepted Solution

    > I don't have access to the .conf

    So the only way to mapp the request would be mod_rewrite.

    RewriteEngine on
    # check the HTTP-Host. It must not begin with www.
    RewriteCond %{HTTP_HOST} !^www\.
    # check for a third-level domain
    RewriteCond %{HTTP_HOST} ^[^.]+\.example\.com
    # prevent looping
    RewriteCond $1 !^specialfolder/
    RewriteRule ^(.*) /specialfolder/$1 [L]
    LVL 23

    Assisted Solution


    Better way of getting this done is:

    # Rewrite <subdomain><path> to<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} ^([^.]+)\.example\.com(: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]


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Introduction As you’re probably aware the HTTP protocol offers basic / weak authentication, which in combination with the relevant configuration on your web server, provides the ability to password protect all or part of your host.  If you were not…
    Over the last year I have answered a couple of basic URL rewriting questions several times so I thought I might as well have a stab at: explaining the basics, providing a few useful links and consolidating some of the most common queries into a sing…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now