You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何仅排除单层级路径?WordPress重写规则调整问询

Adjusting WordPress Rewrite Rule to Allow Multi-Level Paths Only

Got it, let's fix this rewrite rule for you. Your original rule was excluding paths starting with numbers under $slug, but now you want to block all single-level paths (regardless of their content) while allowing any multi-level paths (like $slug/anything/anything/ or deeper).

Here's the modified rule you need:

add_rewrite_rule( "^$slug/(?:[^/]+/)+[^/]+/?$", 'index.php?post_type=xxx', 'top' );

Let's break down how this works:

  • ^$slug/: Matches the base $slug path exactly at the start of the URL.
  • (?:[^/]+/)+: This non-capturing group matches at least one segment followed by a slash—this ensures we have at least one subdirectory level, which automatically filters out all single-level paths.
  • [^/]+/?$: Matches the final segment of the multi-level path, with an optional trailing slash to cover both $slug/a/b and $slug/a/b/ formats.

Important Post-Setup Step:

After updating the rule, you need to flush WordPress's rewrite rules cache to make the change take effect:

  • Head to your WordPress admin dashboard → Settings → Permalinks
  • Click the "Save Changes" button (you don't need to edit any settings here—this action triggers the rule flush)

内容的提问来源于stack exchange,提问作者matbro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:03:27