开发者

Forward a subdomain to a subfolder and preserve subfolder's .htaccess mod_rewrite rules

I have a standard LAMP setup and I'm trying to make subdomains automatically work as long as the appropriately-named folder exists.

If you're familiar with MediaTemple's GridServer shared hosting service, I'm trying to emulate the way it handles wildcard subdomains:

  • If subdomain.domain.com is requested, look for a folder named subdomain.domain.com
    • If the folder is found, call it home and serve up the site inside of it, obeying any .htaccess files that may be in there.
    • If no folder is found, just display domain.com

I've been told a .htaccess mod_rewrite in my root domain is the way to go, and I've been able to detect subdomains and at least point to the appropriate subfolder, but I don't think this leaves any opportunity for the subdomain.domain.com .htaccess file's own mod_rewrite to take over once the server knows where the subdomain's folder is located.

Here's my .htaccess for that:

RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9-]+)\.domain.com$
RewriteRule ^(.+) /vhosts/%1.domain.com/html/pages/$1.php [L,QSA]

...And my folder structure:

domain.com
    html
        .htaccess
        vhosts
            subdomain.domain.com
                html
                    .htaccess
                    pages
                        index.php
                        pagename.php

So as you can see, the site inside subdomain.domain.com is dependent on it's own mod_rewrite in order to function, since the page files aren't where the server expects them.

Now, I know I could probably include the subdomain's .htaccess rules in my root domain's .htaccess with the appropriate conditions, but the kicker is that 开发者_如何学PythonI need to be able to point completely different domain names to these subdomains too (so the subdomain is accessible via subdomain.domain.com and mydomain.com), so these subdomain.domain.com folders need to be totally self-sufficient.

So how can I get my server to look in the correct location for a subdomain's folder, while allowing its own .htaccess mod_rewrites to work?

Any help is greatly appreciated!


Just had someone suggest that mod_rewrites are the wrong way to go about it, and mod_vhs is what I want to use. Does anyone know about mod_vhs?


I got it to work using VirtualHosts:

NameVirtualHost *

# Root domain homepage
<VirtualHost *>
    DocumentRoot /var/www/domain.com/html
    ServerName domain.com
    ServerAlias www.domain.com
</VirtualHost>

# Hosted sites
<VirtualHost *>
    VirtualDocumentRoot /var/www/vhosts/%0/html
    ServerName *
    ServerAlias *
</VirtualHost>

The first entry catches requests to my home page and serves up the standard site, while the second catches EVERYTHING else and routes to a different location based on the host name requested.


Easy. Just make sure the subdomain is registered with your server's DNS, then point it with an htaccess like this:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.domain.com$ [OR] 
RewriteCond %{HTTP_HOST} ^(www\.)?sub\.domain\.com
RewriteRule .* /subfolder [L]

No need to mess with the subfolder's htaccess.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜