✨ Professional WordPress development, custom website builder, efficient on-line, cooperate and enjoy optimization services! 🚀

WordPress 404 page after move (web hosting)

当 WordPress 搬家后出现 只有首页能打开,其他页面都是 404 错误 的情况,常见原因是 固定链接设置失效(.htaccess 文件未更新)或伪静态规则没有生效The

你可以通过以下几种方式通过代码直接修复这个问题:

✅ 一、通过代码重写 .htaccess(Apache 环境)

将以下代码添加到主题的 functions.php 中(只需要执行一次,建议加个判断,避免多次执行):

add_action('init', function () {
    $htaccess_path = ABSPATH . '.htaccess';

    // 检查 .htaccess 是否存在
    if (!file_exists($htaccess_path) || strpos(file_get_contents($htaccess_path), 'WordPress') === false) {
        $rules = <<<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
HTACCESS;

        file_put_contents($htaccess_path, $rules);
    }
});


✅ 二、用代码刷新固定链接结构(后台设置等效操作)

你也可以在代码中强制刷新一下固定链接的规则:

add_action('init', function () {
    if (get_option('permalink_structure') !== '/%postname%/') {
        update_option('permalink_structure', '/%postname%/');
        flush_rewrite_rules();
    }
});

 

📌 注意:你可以把这个代码加上条件判断,比如只运行一次或只在管理员登录时执行。


✅ 三、如果是 NGINX 环境

NGINX 不支持 .htaccess,你需要修改服务器配置文件 /etc/nginx/sites-available/your-site.conf,加入类似规则:

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

修改完记得重启 nginx:

bash
sudo service nginx restart

✅ 四、手动修复方案(推荐在后台设置完成后自动执行 flush)

php
// 加入主题 functions.php 文件中
add_action('after_switch_theme', 'flush_rewrite_rules');

🔁 最佳组合建议:

如果你要自动修复搬家后的问题,可以组合这两段:

php
add_action('init', function () {
    // 设置固定链接结构为 /%postname%/
    if (get_option('permalink_structure') !== '/%postname%/') {
        update_option('permalink_structure', '/%postname%/');
    }

    // 强制刷新伪静态规则
    flush_rewrite_rules();

    // 写入 .htaccess(仅限 Apache)
    $htaccess_path = ABSPATH . '.htaccess';
    if (!file_exists($htaccess_path) || strpos(file_get_contents($htaccess_path), 'WordPress') === false) {
        $rules = <<<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
HTACCESS;

        file_put_contents($htaccess_path, $rules);
    }
});

 


如你使用的是 NGINX + WordPress,.htaccess 无效,就只执行 flush_rewrite_rules() 即可,NGINX 规则需手动配置或由运维处理。

WordPress Support Team
WordPress Support Team

💻 A sincere and meticulous young developer 🎓 specializing in the field of custom foreign trade website development.
🌟 specializes in WordPress website design customization and full-service development. Our unique advantage is not only proficient in website development technology, but also the international mainstream art design elements 🎨 skillful integration, while developing accurate and effective brand marketing strategy 📈.
💡 Services covered:
🔍 WordPress Theme Development for a unique website visual style and user experience.
💻 WordPress website customization, tailor-made exclusive website according to your foreign trade business needs.
Whether it's website architecture construction, interface aesthetic design, or brand promotion strategy, we can provide you with one-stop quality solutions to help your foreign trade business stand out on the Internet and step into the global market 🚀.

Articles: 45

Leave a Reply

Your email address will not be published. Required fields are marked *

Submit your request

Quote Collection Form

💥Website Builder | Strengths speak for themselves, not false!

Treasure, we say, a lot of parties are not happy with their own sites are peers copying homework, which is like you open a store, certainly do not want others to know your sources of supply is a reason, right? So, in order to give customers a full sense of security, we did not put any customer case work on the site of the link ha, this is our protection of customer privacy.

📌 But if there is a sincere desire to do the site of the boss, or treasure you just want to look at our case, then hurry up to add the contact information of our website, we snappy send you a few look at, the main one sincere!

If you have not decided to do what style of website, but also simple, you find a few peers of the site sent to us, according to do on the end of the matter. We choose to imitate the site does not mean that no skill, we just can not stand that kind of invalid communication and cumbersome to make people crazy process. Some parties to do a corporate display site, have to find dozens of suppliers to roll, to be honest, no real technology companies will be there to lick the party. We are not the same, we want to be down-to-earth to the customer to make cost-effective, so that customers are satisfied with the site straight pat. Those kneeling work, let those who only play lip service, no technology companies to do it, we do not serve! # Website Development

# Party A Party B # Rejected Inside Coil

Contact the WordPress Technical Team

We look forward to hearing from you.

Submit your request

Quote Collection Form