1、通过.htaccess强制将http 80转https

  1. RewriteEngine On
  2. RewriteCond %{SERVER_PORT} 80
  3. RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]

2、通过VirtualHost添加重定向
即在VirtualHost节点内,添加如下配置:

  1. RewriteEngine on
  2. RewriteCond %{HTTPS} !=on
  3. RewriteRule ^(.*) https://%{SERVER_NAME}$1 [L,R]

完整的配置如下:

  1. <VirtualHost *:80>
  2. ServerAdmin admin@163.com
  3. DocumentRoot /var/www/html/wordpress
  4. ServerName blog.mimvp.com
  5. RewriteEngine on
  6. RewriteCond %{HTTPS} !=on
  7. RewriteRule ^(.*) https://%{SERVER_NAME}$1 [L,R]
  8. DirectoryIndex index.php
  9. ErrorLog /var/log/blog.shaohualee.com-error_log
  10. CustomLog /var/log/blog.shaohualee.com-access_log common
  11. </VirtualHost>

最终是这样子的:

  1. <VirtualHost *:80>
  2. ServerAdmin admin@example.com
  3. DocumentRoot "/data/wwwroot/www.shaohualee.com"
  4. ServerName www.shaohualee.com
  5. ServerAlias shaohualee.com
  6. RewriteEngine on
  7. RewriteCond %{HTTPS} !=on
  8. RewriteRule ^(.*) https://%{SERVER_NAME}$1 [L,R]
  9. ErrorLog "/data/wwwlogs/www.shaohualee.com_error_apache.log"
  10. CustomLog "/data/wwwlogs/www.shaohualee.com_apache.log" common
  11. <Directory "/data/wwwroot/www.shaohualee.com">
  12. SetOutputFilter DEFLATE
  13. Options FollowSymLinks ExecCGI
  14. Require all granted
  15. AllowOverride All
  16. Order allow,deny
  17. Allow from all
  18. DirectoryIndex index.html index.php
  19. </Directory>
  20. </VirtualHost>
  21. <VirtualHost *:443>
  22. ServerAdmin admin@example.com
  23. DocumentRoot "/data/wwwroot/www.shaohualee.com"
  24. ServerName www.shaohualee.com
  25. ServerAlias shaohualee.com
  26. SSLEngine on
  27. SSLCertificateFile "/usr/local/apache/conf/ssl/2_www.shaohualee.com.crt"
  28. SSLCertificateKeyFile "/usr/local/apache/conf/ssl/3_www.shaohualee.com.key"
  29. ErrorLog "/data/wwwlogs/www.shaohualee.com_error_apache.log"
  30. CustomLog "/data/wwwlogs/www.shaohualee.com_apache.log" common
  31. <Directory "/data/wwwroot/www.shaohualee.com">
  32. SetOutputFilter DEFLATE
  33. Options FollowSymLinks ExecCGI
  34. Require all granted
  35. AllowOverride All
  36. Order allow,deny
  37. Allow from all
  38. DirectoryIndex index.html index.php
  39. </Directory>
  40. </VirtualHost>

大家想用的话,复制一下就可以用了。记得要重启:

  1. systemctl restart httpd

分类: web

标签:   apache