首页 > 解决方案 > Wordpress 重写规则不适用于“name”,但适用于“p”

问题描述

如果我像这样设置 Wodpress 重写规则:

function lf_blog_add_rewrites()
{
    add_rewrite_rule(
        '^blog/([^/]+)$',
        'index.php?name=somepost-slug',
        'top'
    );
}

add_action( 'init', 'lf_blog_add_rewrites' );

我得到一个 404 页面。

以下工作完美,但是......

function lf_blog_add_rewrites()
{
    add_rewrite_rule(
        '^blog/([^/]+)$',
        'index.php?p=4567',
        'top'
    );
}

add_action( 'init', 'lf_blog_add_rewrites' );

我正在尝试链接以获取“/blog/slug-of-post”以链接到该页面。我可能做错了什么?

最后,如果由于某种原因这是不可能的,我可以看到: index.php?name=some-slug 似乎做了 301 重定向到正确的地方。那么如何在不破坏 WP .htaccess 重写规则的情况下设置 URL 重写呢?

标签: phpwordpressurl-rewriting

解决方案


推荐阅读