如何仅排除单层级路径?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$slugpath 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/band$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
相关产品推荐
相关产品推荐

