使用特定域的.htaccess重定向到HTTPS

Dan*_*ern 16 .htaccess https redirect

我目前有2个域可以访问我服务器上的同一文件夹:metrikstudios.com和ziced.com.

我希望通过http://metrikstudios.com输入的用户被重定向到https://metrikstudios.com,并且通过http://ziced.com输入的用户不会被重定向到https://ziced.com.

我目前在我的.htaccess上有这个

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Run Code Online (Sandbox Code Playgroud)

谢谢

Fab*_*geb 31

你可以简单地添加另一个RewriteCond来检查主机是否是metrikstudios.com

RewriteCond %{HTTP_HOST} ^metrikstudios\.com [NC]
Run Code Online (Sandbox Code Playgroud)

它看起来应该是这样的:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^metrikstudios\.com [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}
Run Code Online (Sandbox Code Playgroud)


sta*_*een 7

上面接受的解决方案仅将non-www域从重定向httphttps

如果您想重定向域的wwwnon-www版本,ssl将以下内容RewriteCond放在 http 到 https 规则的正上方或RewriteCond %{HTTPS} off行之前 :

RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC]
Run Code Online (Sandbox Code Playgroud)

这是将特定域重定向到 https 的完整规则。

RewriteEngine on

# First we will check the host header (url)
#if it's www.example.com or example.com
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC]
# now we will check the https header
# if https is off (Is non-ssl)
RewriteCond %{HTTPS} off
#redirect the request to https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
Run Code Online (Sandbox Code Playgroud)