欢迎访问艾立兹站 Aliz- 专注建站教程与技术分享
您当前的位置:首页 > 建站教程 > WordPress 建站

WordPress Nginx伪静态完整配置以及旧文章301重定向实操教程

时间:2026-08-13 22:39:16  来源:aliz.cn  作者:SKY  阅读:
原创实测|适用于 WordPress6.x,Nginx 环境。 WordPress 想要实现友好 URL,不能只在后台设置固定链接,Nginx 必须配置对应的 rewrite 伪静态规则,否则页面全部 404。 同时网站改版、修改固定链接之后,旧链接需要做 301 永久重定向,保护 SEO 权重,本文整合标准配置、子目录部署、301 跳转、去除 category 分类前缀、踩坑排查。

一、WordPress 标准 Nginx 伪静态(根目录部署)


编辑站点 nginx server 配置块
nginx
server {
    listen 443 ssl;
    server_name www.aliz.cn aliz.cn;
    root  /www/wwwroot/aliz.cn;
    index index.php index.html;

    #ssl证书配置省略……

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

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

配置保存,重载 nginx:nginx -t && nginx -s reload
后台:设置‑固定链接,选择:文章名 /%postname%/,保存。此时所有文章、分类、标签伪静态生效。

二、WordPress 安装在子目录场景


例如网站域名 www.aliz.cn/blog/,wp 程序放在 /blog文件夹
nginx
location /blog/ {
    try_files $uri $uri/ /blog/index.php?$query_string;
}

location ~ ^/blog/(.*\.php)$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

三、HTTP 强制跳转 HTTPS 完整 server 示例


nginx
server {
    listen 80;
    server_name www.aliz.cn aliz.cn;
    return 301 https://www.aliz.cn$request_uri;
}

server {
    listen 443 ssl;
    server_name www.aliz.cn aliz.cn;
    root /www/wwwroot/aliz.cn;
    index index.php index.html;

    ssl_certificate xxx.pem;
    ssl_certificate_key xxx.key;

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

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

四、去除分类 url 里面的 category(两种方案)

方案 1:WordPress 后台代码实现(推荐,不用改 Nginx)


主题functions.php加入
php
运行
//移除分类前缀category
add_filter('category_link', function($a){
    return str_replace('/category/','/',$a);
},100,1);

add_action('init',function(){
    $GLOBALS['wp_rewrite']->category_base = '';
});

保存,后台 设置→固定链接,点击保存一次刷新重写规则。

方案 2:Nginx 规则兜底


nginx
rewrite ^/category/(.*)$ /$1 permanent;
重要:去掉 category 之后,旧 /category/xxx 需要 301 跳转,防止产生重复收录。

五、旧 URL 301 重定向几种常用场景

301 永久重定向,传递 SEO 权重;302 临时跳转不传递权重。

场景 1:单条旧文章跳转新地址


nginx
rewrite ^/old‑article\.html$  /new‑article/ permanent;

场景 2:旧日期格式 URL,跳转新文章名格式


旧:/2026/08/12/test.html 新:/test/
nginx
rewrite ^/\d{4}/\d{2}/\d{2}/([^/]+)\.html$ /$1/ permanent;

场景 3:不带 www 跳转带 www


nginx
if ($host = 'aliz.cn') {
    return 301 https://www.aliz.cn$request_uri;
}

场景 4:旧分类 /category/abc 跳转 /abc


nginx
rewrite ^/category/(.*)$ /$1 permanent;
⚠️注意:rewrite 写在 server {} 内部,写在 location 外面。

六、使用 WordPress 插件做 301(不懂 Nginx 的选择)


插件:Redirection 优点:网页后台可视化添加跳转,不用修改 nginx 配置; 缺点:跳转由 PHP 执行,性能略低于 Nginx 层规则;适合少量跳转。

七、.htaccess (Apache 环境) 参考规则


apache
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

八、高频问题排查

1、保存固定链接之后全部 404

  1. Nginx 没有写try_files伪静态规则;
  2. 修改 nginx 配置没有执行nginx‑s reload重载;
  3. 宝塔用户,网站设置伪静态选择 WordPress 模板。

2、后台可以打开,文章前台 404


伪静态规则缺失,重点检查try_files $uri $uri/ /index.php?$query_string;

3、301 跳转循环重定向

  • https 配置错误,$request_uri 重复;
  • 插件与 nginx 同时写跳转,双重跳转冲突,关闭其中一套。

4、去掉 category 后分类页面 404


functions.php 添加代码后,必须进入固定链接页面点击保存,刷新 wp 重写缓存。

5、子目录部署 css/js 样式错乱


网站设置‑常规,WordPress 地址、站点地址填写完整带子目录地址,例如https://www.aliz.cn/blog

九、SEO 配套建议

  1. 修改 URL 结构之后,在百度搜索资源平台、Google Search Console 提交旧链接失效,提交新 sitemap;
  2. 尽量 Nginx 层做 301,性能高于 PHP 插件;
  3. 不要频繁修改固定链接,每次修改都会产生大量旧链接,需要批量跳转。
小结:
  1. Nginx 环境核心是try_files伪静态语句;
  2. URL 改版一定要配置 301 永久重定向保护权重;
  3. 去除 category 前缀优先 PHP 代码,再配合 Nginx 兜底跳转旧链接。

本文配套模板、静态源码可前往艾立兹素材库alisucai.com下载

发表评论 共有0条评论
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
公众号二维码

扫码关注公众号
获取全套技术教程