Apache 强制SSL不使用.htaccess

Apache 强制SSL不使用.htaccess,apache,.htaccess,ssl,Apache,.htaccess,Ssl,我在.htaccess文件中尝试了以下所有操作 #RewriteEngine On #RewriteCond %{HTTPS} off #RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] #RewriteEngine On #RewriteCond %{SERVER_PORT} !443 #RewriteRule ^(/(.*))?$ https://%{HTTP_HOST}/$1 [R=301,L] #Rewri

我在.htaccess文件中尝试了以下所有操作

#RewriteEngine On
#RewriteCond %{HTTPS} off
#RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#RewriteEngine On
#RewriteCond %{SERVER_PORT} !443
#RewriteRule ^(/(.*))?$ https://%{HTTP_HOST}/$1 [R=301,L]

#RewriteEngine On
#RewriteCond %{HTTPS} off
#RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

RewriteEngine On

# If we receive a forwarded http request from a proxy...
RewriteCond %{HTTP:X-Forwarded-Proto} =http [OR]

# ...or just a plain old http request directly from the client
RewriteCond %{HTTP:X-Forwarded-Proto} =""
RewriteCond %{HTTPS} !=on

# Redirect to https version
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

我在这台服务器上有两台虚拟主机,两台都不工作。我在httpd.conf文件中启用了mod rewrite。有什么想法吗?

不建议使用
mod\u rewrite
将HTTP重定向到HTTPS。您应该改为在虚拟主机配置中使用

例如:

<VirtualHost *:80>
   ServerName example.com
   Redirect / https://example.com/
</VirtualHost>

<VirtualHost _default_:443>
    ServerName example.com

    # server settings e.g ProxyPass or DocumentRoot

    SSLEngine On
    # other ssl settings
</VirtualHost>

ServerName example.com
重定向/https://example.com/
ServerName example.com
#服务器设置,例如ProxyPass或DocumentRoot
斯伦金安
#其他ssl设置

如果可以,将配置放在Apache的主文件(或其中包含的文件)中,而不是
.htaccess
。您将获得更高的安全性、更简单性和更好的性能。也许您的主机与nginx一起运行apache,因此您的.htaccess会被某些文件绕过?