环境:Debian 7 x64

首先开启rewrite和ssl模块,然后重启apache以使生效。

a2enmod ssl 
a2enmod rewrite
service apache2 restart

找到默认的站点文件存放地址,本例中为/etc/apache2/sites-enabled/

编辑该目录下的000-default文件

该文件内容较长,截取一部分

<VirtualHost *:80>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None

<---中间省略--->


        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

在原本的配置文件基础上,将本来的80改成443,并增加以下几行:

SSLEngine on
SSLCertificateFile /where/your/cert/locates     //你证书的路径
SSLCertificateKeyFile /where/your/key/locates   //你私钥的路径
SSLCACertificateFile /where/your/CA/locates(optional,in case the certificate chain is broken)     //你中级证书的路径(可选,如果证书链不完整时可用)

然后重写80的配置:

<VirtualHost *:80>
	ServerName your_domain_here //你的域名
	ServerAdmin webmaster@localhost //随意
	<IfModule mod_rewrite.c>
		RewriteEngine On
		RewriteCond %{HTTPS} off
		RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
	</IfModule>
</VirtualHost>

保存后重启,访问http://你的域名来检测是不是跳转到了https