I get a 404 error
parent-page/any-child-page/?var1=x&var2=y&var3=z
This is what I have so far:
I want to have the rule cover any child page
add_rewrite_rule('^project-centers/(.+)/(.+)/(.+)/(.+)/?$', 'index.php?pagename=$matches[1]&var1=$matches[2]&var2=$matches[3]&var3=$matches[4]','top');
I can get the values without the rewrite rule using:
/project-centers/kitchen/?var1=x&var2=y&var3=z
Here is the query var_dump:
array(67) {
["pagename"]=>
string(7) "kitchen"
["project_name"]=>
string(7) "kitchen"
["var1"]=>
string(1) "x"
["var2"]=>
string(1) "y"
["var3"]=>
string(1) "z"
["error"]=>
...
What and I missing?
I get a 404 error
parent-page/any-child-page/?var1=x&var2=y&var3=z
This is what I have so far:
I want to have the rule cover any child page
add_rewrite_rule('^project-centers/(.+)/(.+)/(.+)/(.+)/?$', 'index.php?pagename=$matches[1]&var1=$matches[2]&var2=$matches[3]&var3=$matches[4]','top');
I can get the values without the rewrite rule using:
/project-centers/kitchen/?var1=x&var2=y&var3=z
Here is the query var_dump:
array(67) {
["pagename"]=>
string(7) "kitchen"
["project_name"]=>
string(7) "kitchen"
["var1"]=>
string(1) "x"
["var2"]=>
string(1) "y"
["var3"]=>
string(1) "z"
["error"]=>
...
What and I missing?
Share Improve this question asked Jul 27, 2020 at 13:03 mmeehanmmeehan 11 bronze badge 2 |1 Answer
Reset to default 0First thing to check is make sure you visit permalinks page, or call the flush_rewrite_rules
function once after you call add_rewrite_rules otherwise the rule will not get applied.
So to restate your question, is it correct that you're trying to achieve rewriting this:
example/project-centers/kitchen/x/y/z
To this:
eample/index.php?pagename=kitchen&var1=x&var2=y&var3=z
If that's correct, your code looks like it should work, but if it's not you should try writing your rewrite rule like this:
add_rewrite_rule(
'^project-centers/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$',
'index.php?pagename=$matches[1]&var1=$matches[2]&var2=$matches[3]&var3=$matches[4]',
'top'
);
This change is to make it clear to the regex that it should never match a /
inside any of the matched strings, otherwise it can easily do this and things get complicated.
Remember to either visit the permalinks page, or call the flush_rewrite_rules
function once (not on every page load) after you call add_rewrite_rules otherwise the rule will not get applied!
Let me know if that helps
index.php?pagename=kitchen&var1=x&var2=y&var3=z
– mozboz Commented Jul 27, 2020 at 14:03?page_id=9626&var1=x&var2=y&var3=z
Yes, with permalinks set to post name, but the URL converts toproject-centers/kitchen/?var1=x&var2=y&var3=z
– mmeehan Commented Jul 28, 2020 at 2:19