【问题标题】:mod_rewrite URL (condition in second part)mod_rewrite URL(第二部分的条件)
【发布时间】:2013-06-26 20:02:32
【问题描述】:

我有以下重写规则

RewriteRule ^/product/([^/]*)/([^/]*)/([^/]*)/([^/]*)/((test_data|data_test)/)?([^/]*)$ /product/?query_one=$1&query_two=$2&query_three=$3&query_four=$4&file_path=$6/$7

在最后一个file_path 的值中,我正在合并$6/$7

如果$6 没有值,那么file_path 是否可能只是$7?我不想/ id $6 是无。简单来说,我想在这部分添加条件。

编辑

https://example.com/product/one/two/three/four/test_data/file.jar

https://example.com/product/?query_one=one&query_two=two&query_three=three&query_four=four&file_path=test_data/file.jar

这个网址有问题

https://example.com/product/one/two/three/four/test_file.xml

请推荐?

谢谢!

【问题讨论】:

  • 请提供您的源 URL 和目标 URL 的一些示例。
  • @anubhava 问题更新了示例

标签: regex apache url mod-rewrite


【解决方案1】:

看起来你只需要稍微简化你的匹配模式。以下对我有用:

RewriteRule ^/?product/([^/]*)/([^/]*)/([^/]*)/([^/]*)/(.*?)/?$ /product/?query_one=$1&query_two=$2&query_three=$3&query_four=$4&file_path=$5 [L,QSA,NC]

或者,如果您想确保 dir 仅是 (test_data|data_test)然后使用:

RewriteRule ^/?product/([^/]*)/([^/]*)/([^/]*)/([^/]*)/((?:(?:test_data|data_test)/)?.*?)/?$ /product/?query_one=$1&query_two=$2&query_three=$3&query_four=$4&file_path=$5 [L,QSA,NC]

【讨论】:

  • 不,我想对这两种类型的 URL 都使用这种模式。如果$6 为空,我不希望file_type 中的/ 值为空。
  • 除非我自己尝试,否则我从不发布解决方案:P 换句话说,上面的内容适用于您的两个网址。
  • 在这种情况下,任何事情都可能发生(.*?),但我只想提供两个选项(test_data|data_test)/
  • (test_data|data_test) 不会出现在test_file.xml 之前
  • 离开30分钟。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-13
  • 2012-04-29
  • 2012-03-24
  • 1970-01-01
相关资源
最近更新 更多