.htaccess 为子域强制 http

标签 .htaccess ssl mod-rewrite https apache2

我已将我的主站点设置为使用 https。我强制用户使用 .htaccess 访问 https。我用这个代码

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI} [R=301,L]

但是我有一个子域。这就是所谓的 xxx 。它的内容位于 ./xxx 中。如何强制 xxx.domain.com 仅使用 http?

最佳答案

你可以用这个

RewriteEngine on 

#redirect www urls to non-www.
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# do nothing if sub.domain.com is requested 
RewriteCond %{HTTP_HOST} ^sub.domain.com$
RewriteRule ^ - [L]
#redirect the main domain to https
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI} [R=301,L]

在测试之前清除浏览器缓存。

关于.htaccess 为子域强制 http,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48264173/

相关文章:

php - 用PHP模拟文件结构

apache - .htaccess 将参数重定向到子目录

php - CodeIgniter 应用程序中的 500 内部服务器错误

.htaccess 停止内部重定向

wordpress - Cloudflare 525 在 Wordpress 上使用 HTTPS

php - 重写站点上的 URL 地址

python-3.x - 在 sample-app.py 上将 SSL 与 python-instagram 和 localhost 结合使用

c# - 在 AuthenticateAsServer 之前阅读 ClientHello

ssl - 如何让nginx为多个虚拟主机做SSL透传?

regex - Apache ProxyPass - 正则表达式排除文件