将所有子域从 http 重定向到 https [英] redirect all subdomains from http to https
问题描述
我使用以下代码将我的子域的所有 http 请求重定向到 https.
I redirect all http requests for my subdomain to https by using following code.
<VirtualHost *:80>
ServerName subdomain.example.com
Redirect 302 / https://subdomain.example.com
</VirtualHost>
现在我的问题是如何为所有子域执行此操作.
Now my problem is how do I do it for all subdomains.
例如 http:subdomain1.example.com 应该转到 https:subdomain1.example.com 而 http:subdomain2.example.com 应该转到 https:subdomain2.example.com
For example http:subdomain1.example.com should go to https:subdomain1.example.com and http:subdomain2.example.com should go to https:subdomain2.example.com
如何在不必为所有子域创建一个虚拟主机的情况下为所有子域执行此操作
How do I do it for all subdomains without having to create one virtualhost for all of them
更新
我发现 RedirectMatch 采用正则表达式.有人知道如何使用正则表达式吗?
I found that RedirectMatch takes regular expression. Does anyone know how to do it using regex?
推荐答案
您可以将其添加到服务器的 .conf
文件中:
You could add this to your server's .conf
file:
<VirtualHost *:80>
ServerName subdomain.example.com
ServerAlias *.example.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$
RewriteRule ^(.*)$ https://%1.example.com$1 [R=302,L]
</VirtualHost>
ServerAlias 将允许虚拟主机充当通配符,然后您可以从主机标头中提取子域并将它们包含在重写到 https 中
The ServerAlias will allow the vhost to act as a wildcard, then you can extract the subdomain(s) from the host header and include them in the rewrite to https
这篇关于将所有子域从 http 重定向到 https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!