跳至内容

WordPress 固定链接结构只有朴素可用怎么回事

发布日期:
作者:
评论数:暂无评论

如果 WordPress 的固定链接设置只能使用“朴素”选项(即 ?p=123 的结构)正常工作,而使用其他结构(如 /%postname%/)会导致 404 错误,这通常是因为服务器的 URL 重写配置不正确。以下是Apache和Nginx可能的原因和解决方法:

Apache 服务器

  • 启用 mod_rewrite 模块

确保 Apache 的 mod_rewrite 模块已启用,这是 URL 重写的关键组件。

  1. 使用以下命令启用 mod_rewrite
sudo a2enmod rewrite
  1. 重启 Apache 服务器:
sudo systemctl restart apache2
  • 检查 .htaccess 文件

WordPress 依赖 .htaccess 文件来实现 URL 重写。确保文件存在且内容正确。

  1. 进入 WordPress 根目录,查看是否存在 .htaccess 文件。
  1. 确保 .htaccess 文件的内容如下:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

如果文件不存在,可以尝试在 WordPress 管理后台重新保存固定链接设置,这样通常会自动生成 .htaccess 文件。

NGINX 服务器

  1. 配置 URL 重写规则: 在 NGINX 中,重写规则需要在配置文件中手动设置。打开您的 NGINX 配置文件(通常位于 /etc/nginx/sites-available/your-site.conf),确保有以下配置:
   location / {
       try_files $uri $uri/ /index.php?$args;
   }

注意:如果之前的nginx配置里面是try_files $uri $uri/ =404;替换成try_files $uri $uri/ /index.php?$args;即可,这两个不能同时存在只能选其一。

  1. 重启 NGINX: 修改完配置后,重启 NGINX 以应用更改:
   sudo systemctl restart nginx

总结

大多数情况下,固定链接问题是由于服务器的 URL 重写配置不正确造成的。通过正确设置 .htaccess 文件(对于 Apache)或 NGINX 配置文件,通常可以解决这些问题。重启服务器、保存固定链接设置、查看日志文件也有助于诊断和解决问题。

发表回复

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