$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>url rewriting - Custom Params in URL|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

url rewriting - Custom Params in URL

matteradmin8PV0评论

Here is the URL I currently Have working :

www.Mysite?b=ex%2021

I thought I could just add some rules in the .htaccess file, so i added this :

RewriteEngine On
RewriteRule /b/(.*)/$ index.php?b=$1

When i visit www.Mysite/b/ex1/ I get forwarded to the front page, after playing around for a while i discovered I either get Error 404 or a redirect to the main page depending on how i set up the rule.

I guess this wont work because this Wordpress is taking over and messing things up - so so i begin to look into how to do this the WP way, and tried this from some code examples i found :

// rewrite
add_filter( 'query_vars', 'addnew_query_vars', 10, 1 );
function addnew_query_vars($vars)
{   
    $vars[] = 'b';    
    return $vars;
}
function custom_rewrite_basic() 
{
add_rewrite_rule('b/(.*)/', 'b/?b=$1', 'top');
}
add_action('init', 'custom_rewrite_basic');

But I get page not found... I have updated/flushed out the page rules. Should both methods work? Is my code wrong for either?

EDIT : thanks for the help, I have updated the question to show the new rewrite rule. I have tried multiple regex, I believe the above should be working. If i type in myurl/b/ex2/ i get page not found. If i type in myurl/b/22/ it seems to work? - So i tried (a-zA-Z0-9) and with that i get 404. the code i posted above validates as i need in a regex validator.

Here is the URL I currently Have working :

www.Mysite?b=ex%2021

I thought I could just add some rules in the .htaccess file, so i added this :

RewriteEngine On
RewriteRule /b/(.*)/$ index.php?b=$1

When i visit www.Mysite/b/ex1/ I get forwarded to the front page, after playing around for a while i discovered I either get Error 404 or a redirect to the main page depending on how i set up the rule.

I guess this wont work because this Wordpress is taking over and messing things up - so so i begin to look into how to do this the WP way, and tried this from some code examples i found :

// rewrite
add_filter( 'query_vars', 'addnew_query_vars', 10, 1 );
function addnew_query_vars($vars)
{   
    $vars[] = 'b';    
    return $vars;
}
function custom_rewrite_basic() 
{
add_rewrite_rule('b/(.*)/', 'b/?b=$1', 'top');
}
add_action('init', 'custom_rewrite_basic');

But I get page not found... I have updated/flushed out the page rules. Should both methods work? Is my code wrong for either?

EDIT : thanks for the help, I have updated the question to show the new rewrite rule. I have tried multiple regex, I believe the above should be working. If i type in myurl/b/ex2/ i get page not found. If i type in myurl/b/22/ it seems to work? - So i tried (a-zA-Z0-9) and with that i get 404. the code i posted above validates as i need in a regex validator.

Share Improve this question edited Feb 21, 2019 at 15:24 Matt Egginton asked Feb 20, 2019 at 19:20 Matt EggintonMatt Egginton 11 bronze badge 1
  • For starters, your regex is using '[0-9]+' so it won't match /b/ex1, you'd need `[a-z0-9]+' instead. – Alexander Holsgrove Commented Feb 20, 2019 at 19:45
Add a comment  | 

1 Answer 1

Reset to default 0

The regex you have in the rewrite rule, ([0-9]+), will not match your query string ex%2021. Try ?b=21 and see if that works.

Also, new rewrite rules require that the rewrite rules are flushed. You can do this one of two ways. Either call flush_rewrite_rules(); or go to Settings > Permalinks and click save.

Please note, you DO NOT want flush_rewrite_rules(); to be called on a live site, so remove it when you're done developing.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far