Wordpress 重写网址

问题描述 投票:0回答:1

我想将

http://website.com/?slug=product_info.php&products_id=32
重写为
http://website.com/product_info/32
并且我使用此代码,

add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
add_filter('query_vars','wp_insertMyRewriteQueryVars'); 


// Adding a new rule
function wp_insertMyRewriteRules($rules)
{
    $newrules = array();
    $newrules['(product_info)/(\d*)$'] = 'index.php?pagename=store&slug=product_info.php&products_id=$matches[1]';
    return $newrules + $rules;
}

// Adding the bid var so that WP recognizes it
function wp_insertMyRewriteQueryVars($vars)
{
    array_push($vars, 'products_id');
    return $vars;
}

我也用过这个代码,

add_action( 'init', 'wpse41778_add_rewrite' );
function wpse41778_add_rewrite()
{
    add_rewrite_tag('%products_id%','([^/]+)');
    add_rewrite_tag('%slug%','([^/]+)');
    add_rewrite_rule(
        '^product_info/([^/]+)/?$', 
        'index.php?pagename=store&slug=product_info.php&products_id=$matches[1]',
        'top'
    );
}

我已经手动刷新了规则,但是当我转到

http://website.com/product_info/32
时,它转到了
http://website.com
而不是产品页面。两个代码都有这样的问题。

这是我的.htaccess 的内容,

# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# uploaded files
RewriteRule ^files/(.+) wp-includes/ms-files.php?file=$1 [L]
RewriteRule ^login$ /wp-login.php [L]
RewriteRule ^signup$ /wp-signup.php [L]
RewriteRule ^register$ /wp-register.php [L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule . index.php [L]

我不知道问题是什么,我实际上是新手。请帮忙。

php wordpress url-rewriting
1个回答
0
投票

规则不会刷新,添加此代码以自动刷新它们:

add_action('wp_loaded', 'my_flush_rules');

// flush_rules() if our rules are not yet included
function my_flush_rules(){
    $rules = get_option('rewrite_rules');

    if (!isset($rules['(product_info)/(\d*)$'])) {
        global $wp_rewrite;
        $wp_rewrite->flush_rules();
    }
}

并确保您的 .htaccess 文件可由 Wordpress 写入

© www.soinside.com 2019 - 2024. All rights reserved.