You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress迁移后固定链接异常,如何实现无index.php的%postname%格式?

解决WordPress迁移后固定链接(%postname%)子页面404问题

步骤1:启用Apache重写模块

WordPress的美观固定链接依赖Apache的mod_rewrite模块,先确认模块已启用:

# Debian/Ubuntu系统
sudo a2enmod rewrite
sudo systemctl restart apache2

# CentOS/RHEL系统
sudo ln -s /etc/httpd/conf.modules.d/00-base.conf /etc/httpd/conf.d/00-base.conf
sudo systemctl restart httpd

步骤2:修改站点配置文件(mypage.com.conf)

编辑你的站点配置文件,确保目录块允许.htaccess生效(添加AllowOverride All):

<Directory "/var/www/html/mypage">
        Options FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

修改完成后重启Apache服务,让配置生效。

步骤3:配置固定链接并生成/修复.htaccess

  1. 登录WordPress后台,进入设置 → 固定链接,选择「文章名」(对应结构/%postname%/),点击「保存更改」。此时WordPress会自动在网站根目录生成.htaccess文件。
  2. 如果自动生成失败,手动创建.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
  1. 调整.htaccess的权限,确保Apache能读取:
sudo chown www-data:www-data /var/www/html/mypage/.htaccess
sudo chmod 644 /var/www/html/mypage/.htaccess

验证

完成以上步骤后,访问任意子页面(如https://www.mypage.com/postname/),确认页面能正常加载。

内容的提问来源于stack exchange,提问作者Boris Krajnc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 19:40:02