HTTP到HTTPS重写规则不起作用
ubuntu 14.04
阿帕奇/ 2.4.7
我在这里发布我的虚拟主机和默认ssl主机的conf文件。 无法弄清楚我做错了什么。
http://
显示文件夹的索引。 我想将其重定向到https。
https://
打开正常。
重要说明:我尚未启用默认SSL站点。
cat default-ssl.conf|grep -v "#" ServerAdmin webmaster@localhost DocumentRoot /var/www/html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined SSLEngine on SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key SSLOptions +StdEnvVars SSLOptions +StdEnvVars BrowserMatch "MSIE [2-6]" \ nokeepalive ssl-unclean-shutdown \ downgrade-1.0 force-response-1.0 BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
这是mywebsite配置文件:
cat www.mywebsite.com.conf|grep -v "#" ServerName www.mywebsite.com:443 ServerAlias www.mywebsite.com ServerAdmin abc@mywebsite.com DocumentRoot /var/www/www.mywebsite.com/html Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains" RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} SSLEngine on SSLEngine on SSLCertificateFile /etc/apache2/ssl/apache.crt SSLCertificateKeyFile /etc/apache2/ssl/apache.key SSLOptions +StdEnvVars SSLOptions +StdEnvVars BrowserMatch "MSIE [2-6]" \ nokeepalive ssl-unclean-shutdown \ downgrade-1.0 force-response-1.0 BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown ErrorLog ${APACHE_LOG_DIR}/ssl.error.log CustomLog ${APACHE_LOG_DIR}/ssl.access.log combined
如果您希望始终通过https
发送http://www.mywebsite.com/
,则应使用redirect
因为使用mod_rewrite
不是推荐的行为。
根据Redirect Request to SSL Apache wiki页面:
使用SSL时,您经常会有至少两个虚拟主机:一个在端口80上为普通请求提供服务,另一个在端口443上为SSL提供服务。 如果您希望将用户从非安全站点重定向到SSL站点,则可以在非安全VirtualHost中使用普通的Redirect指令
因此,尝试在非安全的VirtualHost中添加此指令:
Redirect permanent / https://www.mywebsite.com/
如果你想要使用rewrite
规则,你应该在非安全的VirtualHost
添加这些行:
RewriteEngine On # This will enable the Rewrite capabilities RewriteCond %{HTTPS} !=on # This checks to make sure the connection is not already HTTPS RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L] # This rule will redirect users from their original location, to the same location but using HTTPS. # ie http://www.mywebsite.com/foo/ to https://www.mywebsite.com/foo/
如HTTP到HTTPS Apache wiki页面中所述。
您的配置不起作用,因为它未定义处理http请求并将其重定向到安全VirtualHost的非安全VirtualHost(通常在端口80上)。
尝试添加以下行:
ServerName dev.dom1.com Redirect permanent / https://dev.dom1.com/
在这种情况下,您不需要DocumentRoot
因为此VirtualHost
正在重定向所有内容。
配置文件中显示的Rewrite
规则保护安全的VirtualHost
不被http
协议访问,例如http://www.mywebsite.com:443/
将是https://www.mywebsite.com:443/
您还应该检查您的网站是否链接到HTML页面中的正确页面(https)。
这是一个旧post,但在Ubuntu 14.04中,原始重写有效,您只需将其更改为:
RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}