url rewriting - Custom Params in URL

admin2025-06-03  4

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748880202a314490.html

最新回复(0)