apache实现http自动转为https

1.确认此服务器apache已经配置过证书可以用https访问到

2.确认/etc/apache2/ports.conf 下有

Listen 80
Listen 443(此条)

3.配置虚礼主机定将域名位到指定文件夹

<VirtualHost *:80>

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/laravel/public
        ServerName www.uipxw.com

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

<VirtualHost *:443>

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/laravel/public
        ServerName www.uipxw.com

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

  两条记录相同监听端口不同

4.无框架在需要转为https的入口新建。.htaccess文件

写入

<IfModule mod_rewrite.c>
    RewriteEngine On

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

</IfModule>

5.有框架在需要转为https的入口新建。.htaccess文件

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# BEGIN WordPress
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

  如果不强制转为https框架的(.htaccess文件的内容应该是)

<IfModule mod_rewrite.c>
  Options +FollowSymlinks -Multiviews
  RewriteEngine On

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L]
</IfModule>

 

原文地址:https://www.cnblogs.com/qqlong/p/8430248.html