【问题标题】:Regex matching everything before the question mark正则表达式匹配问号前的所有内容
【发布时间】:2012-12-05 21:37:55
【问题描述】:

好的,我正在尝试重写 .htaccess 中的 URL

现在的 URL 如下所示:

http://host.com/catalog/search/item/465?item=465

我希望它看起来像

http://host.com/catalog/search/item/465

我的重写如下,这似乎适用于regex tester

RewriteRule search/item/([^?]+) http://host.com/catalog/search/item/$1 [R=301,L]

但我得到的是:

"This page isn't redirecting properly."

【问题讨论】:

  • 你这里没有死循环吗?您还没有限制规则的匹配部分,所以只要它在 URL 中的任何位置找到“搜索/项目”,就会触发该规则。

标签: regex .htaccess codeigniter


【解决方案1】:

试试这个:

RewriteRule ^([^?]*)\?.* $1 [R=301,L]

【讨论】:

  • 我可能使用不正确,但这符合我的整个路径。 regexpal.com/?flags=g&regex=(.*)%5C%3F.*&input=http%3A%2F%2Fhost.com%2Fcatalog%2Fsearch%2Fitem%2F465%3Fitem%3D465
【解决方案2】:

直到第一个“?”

(^.*)?\?

【讨论】:

  • 哦,我想我应该说“最多但不包括”
  • 事实上,您的 $1 正是表达式在第一个“?”之前得到的。但这可以给你想要的! ;) (^.*)(?=\?)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-31
  • 2011-04-13
  • 2011-05-25
  • 1970-01-01
相关资源
最近更新 更多