在Wordpress中只使用正则表达式匹配一个URL段

在Wordpress中只使用正则表达式匹配一个URL段

问题描述:

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?

我想在Wordpress中重写这些URL: p>

  http  ://localhost/one/.../ 
http:// localhost / one / ... 
  code>  pre> 
 
 

使用以下代码: p> \ n

  add_rewrite_tag('%my_test%','([^ /] *)'); 
 
add_rewrite_rule(
'^ one /([^ /] *)/?',  
'index.php?page_id = 0& my_test = $ matches [1]',
'top'
); 
  code>  pre> 
 
 

它有效,但是 它还允许以下URL: p>

  http:// localhost / one / ... / ... 
http://localhost/one/.../ ..  ./...

我如何只重写 / one /.../ code>和 / one / .. 。 code> URL并返回404 / one /.../.../ code> etc? p> div>

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.

enter image description here