Forum Moderators: phranque

Message Too Old, No Replies

Subdomains and file references in htaccess

Setting up htaccess files in a file hierarchy that contains subdomains

         

denrocs

10:42 am on Jun 9, 2012 (gmt 0)

10+ Year Member



I've researched and tried to come to some conclusions for how file references work in htaccess directives, that are processed for subdomain requests. I'm seeking confirmation or additional insight ...

This is motivated by:
  • wanting a static configuration specified in domain's root htaccess, that will also apply to subdomains (whose root directory are in subdirectories below domain root)
  • not wanting to have any specific configuration required within subdomain file hierarchy (either htaccess or symlinks)
  • not wanting external redirects for certain cases (such as an error document or a site-wide maintenance page--that are used both for domain and subdomains)


All mentioned below is in the context of:
  • Apache 2.2 (interested in knowing about any differences in later versions--as it relates to this discussion)
  • Subdomain requests
  • Subdomains are hosted on the same server as the domain and exist in subdirectories below the domain root
  • In my particular configuration, htaccess files in ancestor directories--above the subdomain root--ARE being read and processed for subdomain requests
  • htaccess being only option available for altering server configuration, and thus only referring to htaccess-allowed directives
  • There are no mod_rewrite rules for redirecting subdomain requests (automatically handled by hosting configuration)


My conclusions are:

All absolute file/url references (in directives) are relative to subdomain root -- even for directives applied from htaccess files in ancestor dirs (ancestor to subdomain root)

File/url references in htaccess files have no way of referencing files outside of subdomain file hierarchy (without using fully qualified reference with domain host name) -- even for directives applied from htaccess files in ancestor dirs

Meaning, there are no means within Apache to have a directive reference a file outside of the subdomain file hierarchy (without an external redirect) -- even for directives applied from htaccess files in ancestor dirs

So the only ways for directives to actually/effectively reference files outside of subdomain file hierarchy are:
-- using symlinks within subdomain file hierarchy
-- using php/other scripts/programs (within subdomain) to access files outside subdomain hierarchy

And if that's to be avoided (part of original motivation), either:
-- Subdomains need to reflect/mirror same files/file structure as domain (in terms of directive file references in domain htaccess files, for working the same for both domain and subdomain requests)
-- Or htaccess files in ancestor dirs need to have different sets of directives for subdomains (and since neither Directory or Alias is allowed in htaccess, must use mod_rewrite, and even so file/url references are still relative to and constrained by the subdomain file hierarchy)

lucy24

11:08 am on Jun 9, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



All of that can be expressed in about three sentences:

When a file is physically located in a particular directory, requests for that file have to get past all htaccess files in parent directories, starting with the server's config file. (Same principle as multiple domains in a single userspace: requests don't simply teleport to their final destination, even when the DNS is set up to ensure that they arrive there in the end.)

If mod_rewrite's target does not include the full protocol-plus-domain, it reappends whatever was there in the first place. In this respect, subdomains are no different from URLs with or without leading www or trailing port numbers.

Rules that apply to all subdomains, like Allow and Deny directives (or equivalent in [F]), go in the highest available htaccess. Rules that apply only to one subdomain go in the directory the subdomain lives in.