Apache 将所有子域从http重定向到https
我使用以下代码将我的子域的所有http请求重定向到httpsApache 将所有子域从http重定向到https,apache,redirect,subdomain,wildcard-subdomain,Apache,Redirect,Subdomain,Wildcard Subdomain,我使用以下代码将我的子域的所有http请求重定向到https <VirtualHost *:80> ServerName subdomain.example.com Redirect 302 / https://subdomain.example.com </VirtualHost> ServerName subdomain.example.com 重定向302/https://subdomain.example.com 现在我的问题是如何对所有子域执行此操
<VirtualHost *:80>
ServerName subdomain.example.com
Redirect 302 / https://subdomain.example.com
</VirtualHost>
ServerName subdomain.example.com
重定向302/https://subdomain.example.com
现在我的问题是如何对所有子域执行此操作
例如http:subdomain1.example.com应转到https:subdomain1.example.com,http:subdomain2.example.com应转到https:subdomain2.example.com
如何对所有子域执行此操作,而不必为所有子域创建一个虚拟主机
更新
我发现重定向匹配采用正则表达式。有人知道如何使用正则表达式吗?您可以将其添加到服务器的
.conf
文件中:
<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>
ServerName subdomain.example.com
ServerAlias*.example.com
重新启动发动机
重写cond%{HTTP\u HOST}^(+)\.example\.com$
重写规则^(.*)$https://%1.example.com$1[R=302,L]
ServerAlias将允许vhost充当通配符,然后您可以从主机头提取子域,并将其包含在重写为https中。下面是对.conf文件的一个更简单的通用修改:
<VirtualHost *:80>
#...whatever you already have set up...
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
</VirtualHost>
#…不管你已经设置了什么。。。
重新启动发动机
重写条件%{HTTP_HOST}^(.*)$[NC]
重写规则^https://%1%{REQUEST_URI}[L,NE,R=301]
作为各州的Apache Wiki条目
使用mod_rewrite执行此操作不是推荐的行为。看
强制HTTP到HTTPS重定向(也适用于子域)的vhost配置如下:
<VirtualHost *:80>
ServerName example.com
ServerAlias *.example.com
<Location "/">
Redirect permanent "https://%{HTTP_HOST}%{REQUEST_URI}"
</Location>
</VirtualHost>
<VirtualHost *:443>
[...your vHost configuration...]
SSLEngine On
SSLCertificateFile /path/to/your/cert.pem
SSLCertificateKeyFile /path/to/your/privkey.pem
</VirtualHost>
ServerName example.com
ServerAlias*.example.com
重定向永久“https://%{HTTP_HOST}%{REQUEST_URI}”
[…您的vHost配置…]
斯伦金安
SSLCertificateFile/path/to/your/cert.pem
SSLCertificateKeyFile/path/to/your/privkey.pem
说明:重定向和重定向匹配通常没有Mod_Rewrite中的变量(如{HTTP_HOST}
),但如果使用
,这些变量将被分配
Redirect permanent
(可选:Redirect 301
)将使用http 301代码重定向,因为
301重定向被认为是将用户从HTTP升级到HTTPS的有效方法
注意:此配置基于for子域。这是一种理论语法,但如果没有签名证书,它将无法工作。谢谢您的回答。我想这对我有用。但是有没有一种更简单的语法,比如“Redirect 302”@drabo2005-什么?这是端口80上的虚拟主机-即,它仅服务于HTTP-此主机上不需要证书…证书是必需的,但不在此虚拟主机中。您的行:
RewriteCond%{HTTP\u host}^(.+)\。子域\.com$
应该是RewriteCond%{HTTP\u host}^(.+)\。示例\.com$
。更简单、更完美!这确实解决了我的问题!这里是推荐信,谢谢!非常感谢。这为Linux新手节省了大量的时间和搜索!这是行不通的。要重定向到的URL丢失,尽管我也认为根据您的引用和我的研究,重写是错误的做法。@PauloNeves可能有不同的原因,错误的vhost配置或不支持该指令的过时apache。尝试将loglevel设置为debug以了解更多信息:@PauloNeves此解决方案在我手中有效。