Web开发与WP

Web 服务器开启自定义伪静态固定链接后页面报 404 的 Nginx / Apache 修复规则

在将 CMS 系统(如 WordPress、Ghost、Typecho)的 URL 结构从动态参数(如 ?p=123)切换为自定义伪静态格式(如 /%postname%/)后,除首页外的所有文章页面一律报 404 Not Found。本文提供 Nginx 与 Apache 的标准路由重写规则。


一、 Nginx 解决方案(try_files 机制)

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

二、 Apache 解决方案(mod_rewrite 机制)

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