将子域转发到子文件夹并保留子文件夹的 .htaccess mod_rewrite 规则

发布于 2024-12-04 14:45:52 字数 1340 浏览 1 评论 0原文

我有一个标准的 LAMP 设置,并且只要适当命名的文件夹存在,我就会尝试使子域自动工作。

如果您熟悉 MediaTemple 的 GridServer 共享托管服务,我正在尝试模拟它处理通配符子域的方式:

  • 如果请求 subdomain.domain.com,请查找名为 subdomain.domain.com 的文件夹
    • 如果找到该文件夹​​,则将其称为主目录并提供其中的网站,并遵循其中可能存在的任何 .htaccess 文件。
    • 如果没有找到文件夹,则仅显示domain.com

我被告知根域中的 .htaccess mod_rewrite 是可行的方法,并且我已经能够检测子域并至少指向适当的子文件夹,但我不要认为一旦服务器知道子域的文件夹所在位置,subdomain.domain.com .htaccess 文件自己的 mod_rewrite 就没有任何机会接管。

这是我的 .htaccess:

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

...以及我的文件夹结构:

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

如您所见,subdomain.domain.com 内的站点依赖于它自己的 mod_rewrite 才能运行,因为页面文件不在服务器所在的位置期待他们。

现在,我知道我可能可以在适当的条件下将子域的 .htaccess 规则包含在我的根域的 .htaccess 中,但更重要的是我也需要能够将完全不同的域名指向这些子域(因此子域是可访问的)通过 subdomain.domain.com 和 mydomain.com),因此这些 subdomain.domain.com 文件夹需要完全自给自足。

那么如何让我的服务器在正确的位置查找子域的文件夹,同时允许其自己的 .htaccess mod_rewrites 工作?

非常感谢任何帮助!


刚刚有人建议 mod_rewrites 是错误的方法,而 mod_vhs 是我想要使用的。有人知道 mod_vhs 吗?

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 I 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开柳相依 2024-12-11 14:45:52

我使用 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>

第一个条目捕获对我的主页的请求并提供标准站点,而第二个条目捕获其他所有内容并根据请求的主机名路由到不同的位置。

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.

披肩女神 2024-12-11 14:45:52

简单的。只需确保子域已在服务器的 DNS 中注册,然后使用 htaccess 指向它,如下所示:

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

无需弄乱子文件夹的 htaccess。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文