I want to rewrite these URLs in Wordpress:
http://localhost/one/.../
http://localhost/one/...
Using the following code:
add_rewrite_tag('%my_test%','([^/]*)');
add_rewrite_rule(
'^one/([^/]*)/?',
'index.php?page_id=0&my_test=$matches[1]',
'top'
);
It works, but it also allows URLs like:
http://localhost/one/.../...
http://localhost/one/.../.../...
How can I rewrite only /one/.../
and /one/...
URLs and return 404 for /one/.../.../
etc?
The '^one/([^/]*)/?',
is matching /one/.../
and /one/...
and /one/(nothing)
. Hovewer anything beyond that is ignored because the regex is not terminated. You need to add $
to the end. And you probably want to replace the *
with a +
if you don't want to match /one/(nothing)
. So, '^one/([^/]+)/?$',
should work.