【问题标题】:Configuring .htaccess for main domain and subdomains using .htaccess only仅使用 .htaccess 为主域和子域配置 .htaccess
【发布时间】:2019-03-12 10:28:23
【问题描述】:

我有一个基于 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 解决方案。

上面的两个代码 sn-ps 都可以正常工作,也不会重定向节点,但只能从主/主文件夹而不是子域重定向。

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

【问题讨论】:

    标签: apache .htaccess redirect proxy subdomain


    【解决方案1】:

    我为任何想在共享主机上使用 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 我希望它对遇到同样问题的人有所帮助。

    【讨论】:

      猜你喜欢
      • 2018-06-05
      • 2014-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-16
      • 2017-01-23
      • 2016-12-19
      • 1970-01-01
      相关资源
      最近更新 更多