3

I am having a bit of trouble rewriting sub-domains to directories that reside above the web root. I have had plenty of experience with mod_rewrite before, but this particular problem is beyond me.

From what I can tell, mod_rewrite is throwing a tantrum because I insist on using relative directories (..) to determine the directory in which the sub-domain files are located.

Unfortunately, there are two restrictions from my client's specifications:

  • Putting the sub-domain as a sub-directory of the web root is not an option. The sub-domain must not be accessible from anywhere except the specific sub-domain (there are likely to be directory clashes). This means http://subdomain.example.com/ must not be accessible from http://example.com/subdomain/ as that directory may be used in the application on the root domain.
  • An absolute path to the sub-domain files is not known to the client as shared hosting will be used.

If anyone could help me with this problem it would be greatly appreciated! I'd love to start using this in future projects as well, it's quite an elegant solution compared to how we currently deal with sub-domains... If anyone can get it working that is!

Edit: Thought it might be useful to point out that on requesting http://subdomain.example.com/ a 400 Bad Request is returned, rather than a 500 Internal Server Error that I expected. Everything works as expected when requesting the root domain.

Current .htaccess file.

# Begin Rewrite Module for http://*.example.com/
# ==============================================
<IfModule mod_rewrite.c>

    # Turn the rewrite engine on.
    RewriteEngine On
    RewriteBase /

    # Map subdomains to their respective directories.
    RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
    RewriteRule (.*) /../public_subdomains/%1/$1 [L]

    # Rewrite all requests for the root domain to always be served through "index.php".
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*) /index.php/$1 [L]

</IfModule>

Current directory structure.

/
    application/
    cgi-bin/
    framework/
    public_html
        public/
            css/
            images/
            js/
        .htaccess
        index.php
    public_subdomains/
        mysubdomain/
            index.php
        anothersubdomain/
            index.php
zanbaldwin
  • 999
  • 8
  • 28

1 Answers1

1

How you implement this depends on how your host implements subdomains. Some simply map onto your DOCROOT. Others provide a control panel to allow you to do that subdomain -> subdomain docroot yourself. If (2) applies then what you want is already provided, so I'll assume (1) in this ans.

First point to note is that rewriting in an htaccess per-directory context is really URI mapping botched onto the DOCROOT hierarchy. ".." is not allowed and will throw a 500. In practice your are stuck within the domain's DOCROOT.

So public_domains must be a sub directory of DOCROOT -- in your case public_html.

However, you can still simply prevent any direct access to public_html/public_domains by a simple rule:

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI}         /public_domains(/|$)
RewriteRule ^                      -                 [F,L]

See my Tips for debugging .htaccess rewrite rules for more hints. You only want to barf on requests with public_domains on the entry pass. Also remember that you don't include the leading / and targets are relative to DOCROOT for a base of /.

Community
  • 1
  • 1
TerryE
  • 10,724
  • 5
  • 26
  • 48
  • Still having some trouble with mod_rewrite, but if I can't solve it I'll just make a new question. Thanks for clarifying this up for me! :) – zanbaldwin Feb 11 '13 at 02:15