如果尾随斜杠不存在,我想向特定的URL结构添加一个尾随斜杠。这个URL应该是:
/product/product-#/另一个条件是,如果URL有后续的子文件夹,则不应该将尾随斜杠添加到URL中。这个URL应该是:
/product/product-#/subfolder/subpage因此,下面的示例URL应该以尾随斜杠结尾:
/product/product-1 becomes /product/product-1/
/product/product-2 becomes /product/product-2/
/product/product-3 becomes /product/product-3/
/product/product-4/ remains /product/product-4/因此,下面的示例URL不应该以尾随斜杠结尾:
/product/product-1/subfolder/1456 remains /product/product1/subfolder/1456
/product/product-2/subfolder/6789 remains /product/product-2/subfolder/6789我的尝试这里不起作用,子文件夹后面的斜杠没有在非捕获组中注册。
\/(?!.*(?:subfolder\/[0-9]{4})$)[^\/]+$发布于 2021-06-30 12:56:41
使用您所显示的示例,请尝试使用以下正则表达式。
^(\/[^\/]*\/[^\/]*)\/?$在执行url重写时,需要在替换部分使用$1/。
解释:添加了上面的详细说明。
^ ##Matching from starting of value here.
( ##Starting capturing group from here.
\/[^\/]*\/[^\/]* ##Matching 1 slash followed by values till next slash comes.
##Followed by slash and match all values till next slash comes
) ##Closing capturing group here.
\/?$ ##Matching optional / at the end of value here.https://stackoverflow.com/questions/68195150
复制相似问题