为共享 Drupal 8 代码库的六个站点中的三个强制使用 SSL
Force SSL for three of six sites sharing a Drupal 8 codebase
在我的 /sites 文件夹中,我有六个文件夹代表六个不同的 TLD。这些 TLD 中只有三个(tld1.com、tld2.com、tld3.com)拥有 SSL 证书。所有六个在根目录下共享一个 .htaccess 文件。我知道如何为 .htaccess 中的单个 TLD 强制使用 SSL,但不能为三个 TLD 强制使用 SSL。 (仅供参考,我在 .htaccess 中 rewrite base / 未注释。)我将不胜感激任何建议/指导。谢谢。
Only three of these TLDs (tld1.com, tld2.com, tld3.com) have SSL certs.
那些不是“TLDs” - 这些只是 域 - 它们都共享相同的 TLD!
I know how to force SSL for a single TLD in .htaccess, but not for three TLDs
过程非常相似。事实上,如果它特定于您的服务器,您可能需要修改现有指令。 (不同的服务器可能需要稍微不同的方法来将 HTTP 重定向到 HTTPS,具体取决于它的配置方式。我在下面使用的方法,使用 HTTPS
服务器变量将是最常见的。)
您只需向现有的 HTTP 到 HTTPS 重定向添加一些条件(RewriteCond
指令),以检查所请求的主机名。
例如:
RewriteCond %{HTTPS} !on
RewriteCond %(HTTP_HOST} ^(www\.)?example1\.com [NC,OR]
RewriteCond %(HTTP_HOST} ^(www\.)?example2\.com [NC,OR]
RewriteCond %(HTTP_HOST} ^(www\.)?example3\.com [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
请注意最后一个 RewriteCond
指令中没有 OR
标志。
您也可以结合这些条件,因为您只有 3 个域需要检查,而且它们似乎都共享相同的 TLD(即 .com
)。
RewriteCond %{HTTPS} !on
RewriteCond %(HTTP_HOST} ^(www\.)?(example1|example2|example3)\.com [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
UPDATE: I forgot to mention that I do not / not want "www" to be part of the URL. Can the code you shared above be tweaked to accomplish same?
您可以将其实现为两个单独的重定向在 上述 HTTP 到 HTTPS 重定向之前。一个用于您要重定向到 HTTPS 的域,另一个用于其余域(应该保留在 HTTP 上)。
顺便说一下,所有这些重定向都应该放在之前您可能拥有的任何现有 Drupal 指令。
例如:
# Remove www subdomain from "secure" domains
# Also redirects to HTTPS
RewriteCond %{HTTP_HOST} ^www\.example1\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.example2\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.example3\.com [NC]
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
# Remove www subdomain from other (non-secure) domains
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
# HTTP to HTTPS redirect for "secure" (non-www) domains
RewriteCond %{HTTPS} !on
RewriteCond %(HTTP_HOST} ^example1\.com [NC,OR]
RewriteCond %(HTTP_HOST} ^example2\.com [NC,OR]
RewriteCond %(HTTP_HOST} ^example3\.com [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
前两个规则中的 %1
反向引用匹配域减去 www 子域。
如果您愿意,可以将上面的内容简化为如下内容:
# HTTP to HTTPS and remove www subdomain for secure domains
# Currently assumes all secure domains share the same TLD (ie. ".com")
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %(HTTP_HOST} ^(?:www\.)?(example1|example2|example3)\.com [NC]
RewriteRule ^ https://%1.com%{REQUEST_URI} [R=301,L]
# Remove www subdomain from other (non-secure) domains
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
在我的 /sites 文件夹中,我有六个文件夹代表六个不同的 TLD。这些 TLD 中只有三个(tld1.com、tld2.com、tld3.com)拥有 SSL 证书。所有六个在根目录下共享一个 .htaccess 文件。我知道如何为 .htaccess 中的单个 TLD 强制使用 SSL,但不能为三个 TLD 强制使用 SSL。 (仅供参考,我在 .htaccess 中 rewrite base / 未注释。)我将不胜感激任何建议/指导。谢谢。
Only three of these TLDs (tld1.com, tld2.com, tld3.com) have SSL certs.
那些不是“TLDs” - 这些只是 域 - 它们都共享相同的 TLD!
I know how to force SSL for a single TLD in .htaccess, but not for three TLDs
过程非常相似。事实上,如果它特定于您的服务器,您可能需要修改现有指令。 (不同的服务器可能需要稍微不同的方法来将 HTTP 重定向到 HTTPS,具体取决于它的配置方式。我在下面使用的方法,使用 HTTPS
服务器变量将是最常见的。)
您只需向现有的 HTTP 到 HTTPS 重定向添加一些条件(RewriteCond
指令),以检查所请求的主机名。
例如:
RewriteCond %{HTTPS} !on
RewriteCond %(HTTP_HOST} ^(www\.)?example1\.com [NC,OR]
RewriteCond %(HTTP_HOST} ^(www\.)?example2\.com [NC,OR]
RewriteCond %(HTTP_HOST} ^(www\.)?example3\.com [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
请注意最后一个 RewriteCond
指令中没有 OR
标志。
您也可以结合这些条件,因为您只有 3 个域需要检查,而且它们似乎都共享相同的 TLD(即 .com
)。
RewriteCond %{HTTPS} !on
RewriteCond %(HTTP_HOST} ^(www\.)?(example1|example2|example3)\.com [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
UPDATE: I forgot to mention that I do not / not want "www" to be part of the URL. Can the code you shared above be tweaked to accomplish same?
您可以将其实现为两个单独的重定向在 上述 HTTP 到 HTTPS 重定向之前。一个用于您要重定向到 HTTPS 的域,另一个用于其余域(应该保留在 HTTP 上)。
顺便说一下,所有这些重定向都应该放在之前您可能拥有的任何现有 Drupal 指令。
例如:
# Remove www subdomain from "secure" domains
# Also redirects to HTTPS
RewriteCond %{HTTP_HOST} ^www\.example1\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.example2\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.example3\.com [NC]
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
# Remove www subdomain from other (non-secure) domains
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
# HTTP to HTTPS redirect for "secure" (non-www) domains
RewriteCond %{HTTPS} !on
RewriteCond %(HTTP_HOST} ^example1\.com [NC,OR]
RewriteCond %(HTTP_HOST} ^example2\.com [NC,OR]
RewriteCond %(HTTP_HOST} ^example3\.com [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
前两个规则中的 %1
反向引用匹配域减去 www 子域。
如果您愿意,可以将上面的内容简化为如下内容:
# HTTP to HTTPS and remove www subdomain for secure domains
# Currently assumes all secure domains share the same TLD (ie. ".com")
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %(HTTP_HOST} ^(?:www\.)?(example1|example2|example3)\.com [NC]
RewriteRule ^ https://%1.com%{REQUEST_URI} [R=301,L]
# Remove www subdomain from other (non-secure) domains
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]