Home > Backend Development > PHP Problem > Regarding the problem of setting https in php

Regarding the problem of setting https in php

藏色散人
Release: 2023-03-06 17:18:02
Original
4292 people have browsed it

php设置https的方法:首先设置“httpd.ini”并添加相关配置;然后将“httpd-ssl.conf”里面的“VirtualHost”配置完整路径;最后使http重定向到https即可。

Regarding the problem of setting https in php

推荐:《PHP视频教程

设置https以及http转https的问题

公司用的是阿里云服务器win2008server r2 ,环境是phpwamp,出现许多问题。2018-11-12

一 设置https

1、设置httpd.ini 取消以下三个配置的#

LoadModule socache_shmcb_module modules/mod_socache_shmcb.so 
LoadModule ssl_module modules/mod_ssl.so 
Include conf/extra/httpd-ssl.conf
Copy after login

2、并且注意 httpd-ssl.conf里面的VirtualHost配置要用完整路径,支持多个https同时使用

例如这样既可:

#百度官方网站
<VirtualHost *:443>    
    DocumentRoot "D:/WWW/baidu/public/"    
    ServerName www.baidu.com:443  
    SSLEngine on    
    SSLCertificateFile          D:/ssl/www.baidu.com/www.baidu.com.cer  
    SSLCertificateKeyFile       D:/ssl/www.baidu.com/www.baidu.com.key    
    SSLCertificateChainFile     D:/ssl/www.baidu.com/www.baidu.com_ca.crt  
</VirtualHost>
#淘宝官方网站
<VirtualHost *:443>
    DocumentRoot "D:/WWW/taobao/public/"    
    ServerName www.taobao.com:443  
    SSLEngine on    
    SSLCertificateFile          D:/ssl/www.taobao.com/www.taobao.com.cer  
    SSLCertificateKeyFile       D:/ssl/www.taobao.com/www.taobao.com.key    
    SSLCertificateChainFile     D:/ssl/www.taobao.com/www.taobao.com_ca.crt  
</VirtualHost>
Copy after login
<VirtualHost _default_:443>
    DocumentRoot "C:/*****/PHPWAMP_IN2/wwwroot/test"
    ServerName www.test.com:443
    ServerAlias test.com
    DirectoryIndex index.html index.htm index.php default.php app.php u.php
    ErrorLog logs/example_error.log
    CustomLog logs/example_access.log \
    "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
    SSLEngine on
    SSLCertificateFile "C:/**************.com_public.crt"
    SSLCertificateKeyFile "C:/**********.com.key"
    SSLCertificateChainFile "C:/*****.com_chain.crt"
<Directory "C:/*****/PHPWAMP_IN2/wwwroot/test">
    SSLOptions +StdEnvVars
    AllowOverride All
    Require all granted
</Directory>
<FilesMatch "\.(shtml|phtml|php)$">
    SSLOptions +StdEnvVars
</FilesMatch>
    BrowserMatch "MSIE [2-5]" \
    nokeepalive ssl-unclean-shutdown \
    downgrade-1.0 force-response-1.0
</VirtualHost>
Copy after login

上图是phpwamp软件配置ssl的代码(***是目录路径和文件名 ,这里抹除)

最后要注意的是,查看httpd-ssl.conf文件里面所使用的443端口是否被占用,如果被占用Apache也会启动失败。

3、因为是阿里云服务器,所有需要在阿里云后台管理开启443端口

4、在服务器上的防火墙增加入站规则 443

留意SSLCertificateChainFile的地址配置,三个ssl路径地址中任何一个路径错误都会造成apache的启动失败,如有问题,请重点排查这处。

二 http重定向到https

当你的站点使用了HTTPS之后,你可能会想把所有的HTTP请求(即端口80的请求),全部都重定向至HTTPS(即端口443)。这时候你可以用以下的方式来做到:(Apache mod_rewrite)

把这段代码放在.htaccess文件(网站根目录下),即可实现HTTP到HTTPS的重定向。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.baidu.com/$1 [R=301,L]
</IfModule>
Copy after login

而当你又想用回HTTP的时候,反过来就可以了:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(.*)$ https://www.baidu.com/$1 [R=301,L]
</IfModule>
Copy after login

其中R=301表示Moved Permanently,即告诉搜索引擎或者浏览器下去直接访问后者的地址,

如果只是试验性地重定向,可以使用R=302(Found),临时跳转

The above is the detailed content of Regarding the problem of setting https in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template