分类
学习笔记

WordPress修改permalink后页面出现nginx 404问题

Permalink 是指向网站页面的永久 URL,即网站的完整 URL,它包括网站的域名以及 slug(即域名之后的 URL 部分)。WordPress 安装后之后,默认的 Permalink 结构为 Plain. (e.g. https://zblogs.top/?p=123)

在安装好 WordPress 后,如果修改 Permalink 结构,有可能会导致网站除首页外的其他页面出现 nginx 404 问题,同时,也可能会无法保存文章草稿,发布文章。

本文将大致介绍 Permalink 的相关知识及该问题的一种解决方法。

Permalink

Permalinks are the permanent URLs to your individual weblog posts, as well as categories and other lists of weblog postings. A permalink is what another weblogger will use to link to your article (or section), or how you might send a link to your story in an e-mail message. The URL to each post should be permanent, and never change — hence permalink.

Permalink 是指向网站页面的永久 URL,即网站的完整 URL,它包括网站的域名以及 slug(即域名之后的 URL 部分)。WordPress 安装后之后,默认的 Permalink 结构为 Plain,e.g.

https://zblogs.top/?p=123

Plain 结构的 Permalink 中出现的数字 123, 是 WordPress 为这篇文章特定生成的 ID,它指的是网站数据库中的第 123 篇文章。

虽然 Google 仍然理解这个页面上的内容,但这种格式的 URL 看起来并不专业,对网站的 SEO 也没有任何帮助,因为这个 URL 没有描述页面提供的是什么内容,也不便于链接的分享和传播。

为了让 Permalink 对搜索引擎和读者更加友好,我们会选择 Post name 的结构:

https://zblogs.top/ubuntu-nginx-php-mysql-build-wordpress-site/

如果是新闻网站,那 Permalink 可以选择 Day and name 的结构(传达文章时效性):

https://zblogs.top/2022/01/16/sample-post/

简言之,建议使用简单明了的 Permalink 结构,对大多数网站来说,将文章标题信息附加到 Permalink 中是有意义的,因此推荐使用 Post name 的结构。

如何解决修改permalink后页面nginx 404问题

使用 WordPress 默认的 Plain Permalink 结构,并不会遇到网页 nginx 404 的问题;在修改 Permalink 结构之后,如果遇到页面出现 nginx 404 问题,通常情况下修改 nginx 配置文件就可以解决:

在网站 nginx 配置文件中,在 server {},添加(或修改)如下代码:

location / {
            try_files $uri $uri/ /index.php?$args;
}

完整 server {} 部分代码如下:

server {
        root /var/www/wordpress;
        server_name example.com;
        location / {
                    try_files $uri $uri/ /index.php?$args;
        }
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        }
}

参考文档:

Using Permalinks: https://wordpress.org/support/article/using-permalinks/#using-pretty-permalinks


Justin

Justin(小贾), Microsoft 365 认证:新式桌面管理员助理,乐天派,打工人。

发表回复

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

请输入正确的阿拉伯数字即可 *超出时限。 请再次填写验证码。