Search code examples
apache.htaccessmod-rewritesubdomaindocument-root

RewriteRule to cause subdomain to load another DocumentRoot?


Apache (mostly) noob here... could sure use a little help with what I am trying to do with a RewriteRule in .htaccess: ...to causes URLs using our sharpedge. subdomain to auto-load from the Internet_IE directory - one level deeper than the site root.

I have this in httpd.conf:

[snip]
NameVirtualHost 11.22.33.44

<VirtualHost 11.22.33.44>
    Options All +ExecCGI
    ServerAdmin hostmaster@ourhost.com
    DocumentRoot /var/www/html/ourdomain.com
    ServerName ourdomain.com
    ServerAlias www.ourdomain.com
    DirectoryIndex index.htm index.html index.dna
    #---------------------------------
    <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
    </IfModule>
    #---------------------------------
</VirtualHost>

<VirtualHost 11.22.33.44>
    Options All +ExecCGI
    ServerAdmin hostmaster@ourhost.com
    DocumentRoot /var/www/html/ourdomain.com
    ServerName  sharpedge.ourdomain.com
    DirectoryIndex index.htm index.html index.dna
    #---------------------------------
    <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
    </IfModule>
    #---------------------------------
</VirtualHost>
[snip]

...and this in .htaccess (in the site root, here: /var/www/html/ourdomain.com/)

Options -Indexes

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteCond   %{HTTP_HOST} ^sharpedge\.ourdomain\.com$
    # years ago this next line was used here, but in httpd.conf: (I don't know what it was supposed to do)
    # RewriteRule   ^(.+)        %{HTTP_HOST}$1          [C]
    RewriteRule   ^sharpedge\.ourdomain\.com(.*) /var/www/html/ourdomain.com/Internet_IE/$1  [L]
</IfModule>

..but nothing happens from the RewriteRule; it is as if the RewriteRule was not there.

I'd much appreciate any suggestions.

Edits:

  • I would just change the DocumentRoot to the directory I actually want to serve, but I need to be able to call (include) files from within files in that directory with a prefixing / in the include path, where those files are located in the site root. Those files are also similarly called from other files that are in the site root.

Solution

  • You don't want to put any part of your host in the RewriteRule, but you got the HTTP_HOST right, try:

    RewriteCond %{HTTP_HOST} ^sharpedge\.ourdomain\.com$ [NC]
    # to prevent looping
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /Internet_IE/$1 [L]
    

    Try changing the !-f and !-d checks:

    RewriteCond %{HTTP_HOST} ^sharpedge\.ourdomain\.com$ [NC]
    RewriteCond %{REQUEST_URI} !^/Internet_IE/
    RewriteRule ^(.*)$ /Internet_IE/$1 [L]