0

I have a link like this: https://example.com/page-test/?v=p300 and I created rewrite rule to point new url: https://example.com/page-test/p300/ but it doesnt work.

Just there is redirect to correct website but second parametr "v" is empty. My code:

add_action('init', 'add_test_url');
function add_test_url() {
    add_rewrite_rule('^page-test/([^/]+)/?$', 'index.php?pagename=page-test&v=p300', 'top');
}

So first argument pagename is OK and website is redirected to the correct page but second parametr is empty ... why ? Or course after the change I save the settings permalinks.

1 Answer 1

0

OK I solved this problem:

New code:

add_action('init', 'add_test_url');
function add_test_url() {
    global $wp_rewrite;
    add_rewrite_tag('%v%', '(.+)');
    add_rewrite_rule('^page-test/([^/]+)/?$', 'index.php?pagename=page-test&v=$matches[1]', 'top');
    $wp_rewrite->flush_rules();
}

/// for debug GET value:
function console_log($content) {
    echo "<!-- console_log: " . get_query_var('v') . "-->";
}
add_filter('loop_start', 'console_log', 9);

Only thing left is .htaccess redirects from old url to new url:

<IfModule mod_rewrite.c>
    RewriteRule ^page-test/old_dir/([^/]+)/? page-test/$1 [R=301,L]
</IfModule>

In my case I had make a little change for the requests Ajax script and pass variable from GET request:

$get_example_v = get_query_var('v');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.