如何为 Wordpress 重定向插件匹配一个 URL 中的 2 个查询字符串

How to match 2 query strings from one URL for Wordpress Redirection Plugin

我想使用 WordPress 的 'Redirection' 插件将搜索 URLS 重定向到更清洁的 URL。

重定向插件的来源 URL 接受正则表达式。

我需要的一个例子:

我需要这个搜索 URL:

http://www.danceclass.es/?s=&where=Sydney+%28Australia%29&company_category=pole&cat=pole&search_simple=STEP+3+-+Show+details

重定向到:

http://www.danceclass.es/pole-sydney

但前提是 where=Sydney AND cat=pole.

正则表达式专家可以帮我把这个例子翻译成正则表达式代码吗?

我会冲洗并重复不同的 where=cat=

由于您需要提取这 2 个查询参数的值,因此您需要 2 个捕获组。然而,正则表达式还需要处理不同的查询参数顺序,即 catwhere 顺序可以不同

这是正则表达式
https://regex101.com/r/gP9pZ0/1

您可以使用捕获的组来形成新的 url,例如使用 sed 的替换命令:

$sed -r 's@^.*where=([a-zA-Z]*).*cat=([^&]*).*|.*cat=([^&]*).*where=([a-zA-Z]*).*$@http://www\.danceclass\.es/-@'
http://www.danceclass.es/?s=&where=Sydney+%28Australia%29&company_category=pole&cat=pole&search_simple=STEP+3+-+Show+details
http://www.danceclass.es/pole-Sydney
http://www.danceclass.es/?s=&company_category=pole&cat=pole&search_simple=STEP+3+-+Show+details&where=Sydney+%28Australia%29
http://www.danceclass.es/pole-Sydney
$

一旦你输入了最后的 URL,你就可以将符合你答案的字符串设为小写。给 sed 的 2 个输入有不同的顺序,即第一个 URL 有 where 然后 cat,第二个 URL

的顺序相反

无需遍历不同的值,您可以根据正则表达式中提到的有效字符进行匹配,即:

cat=[^&] cat 值将为您提供每个字符,直到找不到 &
where=[a-zA-Z] 如果 value 是 uppercase/lowercase char

,则 value 将为您提供每个字符