最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

permalinks - How to construct a dynamic rewrite rule for child pages that passes more than one query var

programmeradmin8浏览0评论

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
  • Does this page work ok if you go to it directly? index.php?pagename=kitchen&var1=x&var2=y&var3=z – mozboz Commented Jul 27, 2020 at 14:03
  • Yes, with permalinks set to plain, but the url converts to ?page_id=9626&var1=x&var2=y&var3=z Yes, with permalinks set to post name, but the URL converts to project-centers/kitchen/?var1=x&var2=y&var3=z – mmeehan Commented Jul 28, 2020 at 2:19
Add a comment  | 

1 Answer 1

Reset to default 0

First 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

发布评论

评论列表(0)

  1. 暂无评论