【发布时间】:2012-12-21 04:26:58
【问题描述】:
在 .htaccess 中我需要匹配一个模式(可能的模式,它可能不存在)但得到一个与 $1 不同的值
示例网址:
http://www.example.com/rest-of-the-url
http://www.example.com/en/rest-of-the-url
http://www.example.com/fr/rest-of-the-url
http://www.example.com/it/rest-of-the-url
en/fr/it/部分可能存在也可能不存在
我使用此模式以 en fr ...
的形式匹配语言^([a-z]{2})?other patterns here$
没关系,我得到了 $1 的语言部分。但问题是当没有语言部分时,那么 $1 会从 rest-of-the-url
变成 re如果我使用这种模式
^([a-z]{2}\/)?other patterns here$
使用这种模式,无论语言是否存在,我都可以正确获取到 $1 但以 en/ fr/ 之类的形式,我不想要尾随 /
是否可以像第二个例子一样匹配并得到第一个例子的值?
PS:是的,在 php 中清理尾随 / 非常容易,但我想问是否可以在 htaccess 中执行此操作?
【问题讨论】: