我们有一个通配符 SSL 证书,它应该可以在给定域的任何子域上工作。
所以在这个服务器中我们有这个文件结构:
/home/DOMAIN/public_html/subdomainx
/home/DOMAIN/public_html/subdomainy
等等……
现在,证书已安装,但是当您通过 https 访问任何子域时(例如:hxxps://subdomainx.domain.com)它指向
/home/DOMAIN/public_html/index.php
当您通过 https 访问子域时,我们需要它
hxxps://subdomainx.domain.com
它指向与 http 等效的同一目录: /home/DOMAIN/public_html/subdomainx
我们的提供者告诉我们这是不可能的,当前的行为是正确的,我们应该做一些 htaccess 来实现这一点。
我已经尝试了一些东西,包括这个解决方案,这似乎是我需要的:Advice on Configuring .HTaccess file to Redirect HTTP Subdomain to HTTPS Equivalent
但无法让它工作。
有什么建议吗? 谢谢。
最佳答案
您不希望在重写规则中使用 [R] 标志,或者在目标中使用主机名。您想要重写 URI,以便在其前面加上“subdomainx”。类似的东西:
RewriteEngine On
# We don't want the main domain or www
RewriteCond %{HTTP_HOST} ^((?!www).*)\.domain.com$ [NC]
# Make sure that if we rewrite, the destination actually exists (to prevent loops and properly return 404's)
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -d
# rewrite
RewriteRule ^(.*)$ /%1/$1 [L]
请注意,%{DOCUMENT_ROOT}/%1%{REQUEST_URI}
检查必须以便在不泄露信息的情况下返回正确的 404。当您请求 http://test.domain.com/badfile.html 时,这就是如此,404 消息表示“/badfile.html”未找到,而不是“/test/badfile.html”未找到。
关于.htaccess - htaccess 与通配符 SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9996368/