【问题标题】:Wordpress rewrite rules and custom optional arguments in urlurl 中的 Wordpress 重写规则和自定义可选参数
【发布时间】:2014-04-18 20:46:34
【问题描述】:

我正在尝试使用 add_rewrite_rules 为 Wordpress 网站构建过滤功能。我有以下链接示例

 example.com/offers-type-all-inclusive-from-germany-transportation-airplane.html

使用以下重写规则:

add_rewrite_rule('^offers-type-?([a-z0-9-]+)?-from-?([a-z-]+)?-transportation-?([a-z0-9-]+)?.html','index.php?filters=offers&type=$matches[1]&location=$matches[2]&transportation=$matches[3]','top');

我设法将用户重定向到自定义页面模板,并将参数存储为数组

array(
    'type' => 'all-inclusive',
    'from' => 'germany',
    'transportation' => 'airplane'
     );

问题是某些参数可能是可选的,因此像

这样的链接
example.com/offers-type-all-inclusive-from-germany.html
example.com/offers-type-all-inclusive-transportation-airplane.html

还需要建立正确的过滤数组。

是否有可能实现这一目标?

【问题讨论】:

  • 参数中也用“-”分隔时,是否需要用“-”?为什么不 example.com/offers-type/all-inclusive/from-germany/transportation-airplane?

标签: php regex wordpress url-rewriting rewrite


【解决方案1】:

多么棘手的正则表达式!

试试这个表达式

offers-type-([a-z0-9-]*?)(?:-from-([a-z-]*?))?(?:-transportation-([a-z0-9-]*?))?\.html

这是一个在线演示

http://regex101.com/r/fS4xB7

【讨论】:

  • 它正在工作我只更改为以下regex101.com/r/fS4xB7 但我也注意到将运输部分与它不再工作的位置相反。
猜你喜欢
  • 2015-06-17
  • 1970-01-01
  • 2011-10-22
  • 1970-01-01
  • 2011-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多