首页 > 解决方案 > 仅使用 .htaccess 为主域和子域配置 .htaccess

问题描述

我有一个基于 Apache 2.4 的 www.main.com 主机和多个子域,例如

www.sub1.main.com, www.sub2.main.com, ....

文件夹结构是

/home/main/.htaccess + project files and subdomain folders like:
/home/public-html/main/sub1.main.com/.htaccess
/home/public-html/main/sub1/.htaccess + project1 files
/home/public-html/main/sub2/.htaccess + project2 files

等等

子域是基于不同技术的独立项目

/home/main/ 网站基于 AngularJS 并具有标准 angular .httaccess 配置:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

RewriteRule ^(.*) /index.html [NC,L]

我想在 sub.main.com 中有另一个 .htaccsess 配置,如下所示:

RewriteEngine On
RewriteRule ^$ http://127.0.0.1:3000/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api$ http://127.0.0.1:3000/$1 [P,L]

哪个应该代理将我的 Apache 流量重定向到我的 nodejs 服务器。

在某些时候,我的子域重定向不起作用。这仅适用于主文件夹。

所以我有3个问题:

  1. 我应该为主 .httaccess 中的所有域配置所有规则吗?(由于潜在的 url 冲突,什么不是很好)

  2. 是否可以配置 main .htaccess 只是为了允许其他 sub.domains 使用他们自己的 .httaccess

  3. .httaccess 子域的正确位置在哪里?

这里 - /home/public-html/main/sub1.main.com/.htaccess 或这里 - /home/public-html/main/sub1/.htaccess + project1 文件

这是一个共享主机,对 apache 配置的访问受限。我只能使用 .httaccess 方式,我无权更改我的虚拟主机,因此请仅提供 .httaccess 解决方案。

上面的两个代码片段都有效,并且也没有重定向节点,但仅来自主/主文件夹,而不是来自子域。

我需要每个子域的特殊 .htaccess 来托管多个项目

标签: apache.htaccessredirectproxysubdomain

解决方案


我为任何想要在共享主机上使用 nodeJS 的人找到了解决方案:

放到 /home/main/www/ .htaccess 下面:

RewriteEngine on

RewriteOptions inherit
RewriteCond %{HTTP_HOST} ^main\.org$ [OR]
RewriteCond %{HTTP_HOST} ^www\.main\.org$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/.+$
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^/?$ "http\:\/\/angular\.main\.org\/" [R=301,L]

RewriteBase /
RewriteCond %{REQUEST_URI} !^/node/
# Rewrites all URLS node.main to node server
RewriteCond %{HTTP_HOST} ^(www\.)?node.main\.
# Redirect to node server
RewriteRule ^(.*)$ http://127.0.0.1:3000/$1 [P, L]

在 /home/main/www/angular 里面放 .htaccess 像:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

RewriteRule ^(.*) /index.html [NC,L]

我们不需要节点文件夹中的任何 .htaccess 我希望它对遇到同样问题的人有所帮助。


推荐阅读