Php 将一个URL段与Wordpress中的正则表达式精确匹配
我想在Wordpress中重写这些URL:Php 将一个URL段与Wordpress中的正则表达式精确匹配,php,regex,wordpress,url-rewriting,rewrite,Php,Regex,Wordpress,Url Rewriting,Rewrite,我想在Wordpress中重写这些URL: http://localhost/one/.../ http://localhost/one/... 使用以下代码: add_rewrite_tag('%my_test%','([^/]*)'); add_rewrite_rule( '^one/([^/]*)/?', 'index.php?page_id=0&my_test=$matches[1]', 'top' ); 它可以工作,但也允许URL,如: http:
http://localhost/one/.../
http://localhost/one/...
使用以下代码:
add_rewrite_tag('%my_test%','([^/]*)');
add_rewrite_rule(
'^one/([^/]*)/?',
'index.php?page_id=0&my_test=$matches[1]',
'top'
);
它可以工作,但也允许URL,如:
http://localhost/one/.../...
http://localhost/one/.../.../...
我怎样才能只重写/one/../../
和/one/../../code>URL,并返回404作为/one/../../
等?这个'^one/([^/]*)/?,
匹配/one/../
和/one/../../code>和/one/(无)
。HOWEWER由于正则表达式未终止,因此忽略除此之外的任何内容。您需要在末尾添加$
。如果您不想匹配/one/(nothing)
,则可能需要将*
替换为+
。因此,“^one/([^/]+)/?$”,
应该可以工作