WordPress在centos的nginx环境下切换新主题升级成https后目录和链接做301跳转

导语:一般来说,nginx中升级https后做301跳转大家应该都会,但叶子这次遇到的是客户的机器切换主题后,有部分的链接发生了变化,需要对这些发生了变化的链接也写301跳转规则。

修改虚拟主机文件

使用下面的命令进入虚拟主机配置文件所在的目录,打开nginx虚拟主机配置文件。

cd /www/wdlinux/nginx/conf/vhost

vi test.conf

修改后的nginx虚拟主机配置文件test.conf如下,test.conf为示例,根据自己的实际情况调整。

server {
        listen       80;
        server_name test www.test.com;

        rewrite ^/guide/(.*?)_(.*?)$ https://www.test.com/destination/$1 permanent;
        rewrite ^/guide/(.*?)$ https://www.test.com.com/destinations/$1 permanent;
        rewrite ^/blog/(.*?)$ https://www.test.com/blogs/$1 permanent;
        rewrite ^/feedback/(.*?)$ https://www.test.com/feedbacks/$1 permanent;
        rewrite ^/(.*)$ https://www.test.com/$1 permanent;
}

配置文件解释

该配置文件配置了5条调整规则,rewrite表示跳转url,permanent表示跳转的状态为301。

规则说明

  • 第一条301跳转规则
    rewrite ^/guide/(.*?)_(.*?)$ https://www.test.com/destination/$1 permanent;
    

    原url的格式为:

    http://www.test.com/guide/test/overview_1396
    

    将原url跳转到新url上:

    https://www.test.com/destination/test/overview
    
  • 第二条301跳转规则
    rewrite ^/guide/(.*?)$ https://www.test.com.com/destinations/$1 permanent;
    

    原url的格式为:

    http://www.test.com/guide/test
    

    将原url跳转到新url上:

    https://www.test.com/destinations/test
    
  • 第三条301跳转规则
    rewrite ^/blog/(.*?)$ https://www.test.com/blogs/$1 permanent;
    

    原url的格式为:

    http://www.test.com/blog/test
    

    将原url跳转到新url上:

    https://www.test.com/blogs/test
    
  • 第四条301跳转规则
    rewrite ^/feedback/(.*?)$ https://www.test.com/feedbacks/$1 permanent;
    

    原url的格式为:

    http://www.test.com/feedback/test
    

    将原url跳转到新url上:

    https://www.test.com/feedbacks/test
    
  • 第五条301跳转规则

    这一条规则其实就是把所有http的链接都跳转到https上,状态301。

    rewrite ^/(.*)$ https://www.test.com/$1 permanent;
    

    原url的格式为:

    http://www.test.com/test
    

    将原url跳转到新url上:

    https://www.test.com/test
    

结束

你学会了吗?

发表评论

邮箱地址不会被公开。 必填项已用*标注