个人网站 http 重定向到 https, www 重定向到 non-www 方案

作者: 王炳明 分类: 网站建设 发布时间: 2021-07-06 23:37 热度:1,212

设置 WP 主页域名

进入网站后台,设置站点地址为 https://iswbm.com

个人网站 http 重定向到 https, www 重定向到 non-www 方案

设置两个 A 记录

到你的域名提供商控制台里设置两条 DNS 解析记录

主机记录 记录类型 记录值
@ A ${host-ip}
www A ${host-ip}

修改nginx配置

编辑 /usr/local/nginx/conf/nginx.conf 修改如下两个服务器配置

  • 第一个配置:将 http 重定向到 https(不管带 www 还是不带 www)
  • 第二个配置:接收所有的 https 请求(不管带 www 还是不带 www)
server {
        listen 80;
        server_name iswbm.com www.iswbm.com;
        return 301 https://iswbm.com$request_uri;
        }

server
    {
        listen 443 ssl;

        ssl_certificate 1_iswbm.com_bundle.pem;
        ssl_certificate_key 0_iswbm.com.key;

        server_name _;

        # 剩下的其他配置
    }

记得重启 nginx 使之生效。

当你在使用 https://www.iswbm.com 有可能访问不成功,因为有 301 缓存啥的,使用 F5 强制刷新就可以了。

查看重定向情况

使用 curl -I url 就可以看到地址的重定向情况

$ curl -I http://iswbm.com
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Tue, 06 Jul 2021 15:34:44 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive
Location: https://iswbm.com/


$ curl -I https://iswbm.com
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 06 Jul 2021 15:34:48 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/7.4.9
Set-Cookie: baiduseo_data_category=%7B%22book%22%3A%22baiduseo_category%22%7D; expires=Thu, 05-Aug-2021 15:34:47 GMT; Max-Age=2592000
Set-Cookie: baiduseo_data_paymoney=%7B%22msg%22%3A1%2C%22url%22%3A%22033c2eda0b95dfc04b9c9ef1e7bb631e%22%2C%22status%22%3A1%7D; expires=Wed, 07-Jul-2021 15:34:47 GMT; Max-Age=86400
Set-Cookie: wp-editormd-lang=zh-CN; path=/
Set-Cookie: PHPSESSID=fden169bmmjbopn4rnb89c95rp; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache


$ curl -I http://www.iswbm.com
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Tue, 06 Jul 2021 15:35:03 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive
Location: https://iswbm.com/


$ curl -I https://www.iswbm.com
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Tue, 06 Jul 2021 15:35:08 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.4.9
Set-Cookie: baiduseo_data_category=%7B%22book%22%3A%22baiduseo_category%22%7D; expires=Thu, 05-Aug-2021 15:35:07 GMT; Max-Age=2592000
Set-Cookie: baiduseo_data_paymoney=%7B%22msg%22%3A1%2C%22url%22%3A%22033c2eda0b95dfc04b9c9ef1e7bb631e%22%2C%22status%22%3A1%7D; expires=Wed, 07-Jul-2021 15:35:07 GMT; Max-Age=86400
Set-Cookie: wp-editormd-lang=zh-CN; path=/
Set-Cookie: PHPSESSID=0smke620aseeucca5dlbh31hhs; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
X-Redirect-By: WordPress
Location: https://iswbm.com/

一个小插曲

在最开始的时候,网站后台的站点地址不小心设置错了,导致网站 都访问不了,想把地址修改回来都没办法。

真的很慌,害怕网站 就这么挂了。

不过冷静下来后,我就找到了解决方法。

方法分两步:

第一步:进入 WP 安装目录,替换目录下所有文件的域名信息

cd /home/wwwroot/wordpress/
sed -i "s/www.iswbm.com/iswbm.com/g"  `grep -r www.iswbm.com ./ -l`

第二步:进入数据库,修改数据站点地址

mysql> select * from wp_options limit 5;
+-----------+--------------------+--------------------------------------------------------------+----------+
| option_id | option_name        | option_value                                                 | autoload |
+-----------+--------------------+--------------------------------------------------------------+----------+
|         1 | siteurl            | https://www.iswbm.com                                        | yes      |
|         2 | home               | https://www.iswbm.com                                        | yes      |
|         3 | blogname           | 明哥教程                                                     | yes      |
|         4 | blogdescription    | 专注于 Python Golang Linux 云计算相关的技术分享              | yes      |
|         5 | users_can_register | 1                                                            | yes      |
+-----------+--------------------+--------------------------------------------------------------+----------+

mysql> update wp_options set option_value="https://iswbm.com" where option_id in (1,2);
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

文章有帮助,请作者喝杯咖啡?

发表评论