phpwordpressurl-rewriting

Wordpress rewrite a url


I want to rewrite http://website.com/?slug=product_info.php&products_id=32 to http://website.com/product_info/32 and I use this code,

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;
}

I also used this code,

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'
    );
}

I have manually flushed the rules but when i go to http://website.com/product_info/32 it went to http://website.com instead of the product page. Both of the codes have this kind of issue.

Here is the contents of my .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]

I don't know whats the problem and Im actually newbie on this. Please help.


Solution

  • Rules are not flushed, add this code to flush them automatically:

    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();
        }
    }
    

    and make sure your .htaccess file is writable by Wordpress