htaccess 文件上的正则表达式给出内部重定向错误

Regex on htaccess file gives INTERNAL REDIRECT error

我正在尝试创建一个与以下网址匹配的重定向规则:

en/property/rotterdam/apartment/lorem-ipsum-dolor-sit-amet/v-9/
en/property/rotterdam/apartment/lorem-ipsum-dolor-sit-amet/9/
en/property/rotterdam-centrum/appartment/lorem-ipsum-dolor-sit-amet/9
en/property/lorem-ipsum-dolor-sit-amet-9/
en/property/lorem-ipsum-dolor-sit-amet/v9/
en/property/lorem-ipsum-dolor-sit-amet/v-9/
en/property/lorem-ipsum-dolor-sit-amet/v-9
en/property/rotterdam/apartment/branch/lorem-ipsum-dolor-sit-amet-9/
en/property/rotterdam/lorem-ipsum-dolor-sit-amet-9/
en/property/rotterdam/lorem-ipsum-dolor-sit-amet-9434343/
en/property/rotterdam/apartment/lorem-ipsum-dolor-sit-amet/v-243249/
en/property/rotterdam/apartment/lorem-ipsum-dolor-sit-amet/v2342349/
en/property/rotterdam/apartment/lorem-ipsum-dolor-sit-amet/243249/
en/property/rotterdam/apartment/lorem-ipsum-dolor-sit-amet/v-9/
en/property/rotterdam/apartment/lorem-ipsum-dolor-sit-amet/v-9/?test=1
en/property/rotterdam/apartment/lorem-ipsum-dolor-sit-amet/v-9/test/one/?success=3
en/property/rotterdam/apartment/2-rooms/lorem-ipsum-dolor-sit-amet/v-9/blah/1234/blahh/55555/?sass=123
en/property/rotterdam/appartment/lorem-ipsum-dolor-sit-amet/9234234
en/property/rotterdam/lorem-ipsum-dolor-sit-amet/9234234
en/property/lorem-ipsum-dolor-sit-amet/9234234
en/property/9234234
en/property/rotterdam/appartment/lorem-ipsum-dolor-sit-amet/9/test/132?algo=123

所有需要匹配的url都可以在这里看到
https://regex101.com/r/4TPEhy/1

我面临两个问题第一个是两个网址不匹配:

en/property/rotterdam/apartment/2-rooms/lorem-ipsum-dolor-sit-amet/v-9/blah/1234/blahh/55555/?sass=123
en/property/9234234

第二个,正则表达式中的某些内容导致内部重定向错误。

这是我的正则表达式(到目前为止我得到的最好的情况)

^(en\/)?property\/[A-Яa-я0-9\-_\/]+?([\d]+)(.*)$

这是我的重写规则

RewriteRule ^(en\/)?property\/[A-Яa-я0-9\-_\/]+?([\d]+)(.*)$ properties/property/&%{QUERY_STRING} [NC,L]

正则表达式似乎在工具 (regex101) 上运行良好,但一旦尝试刷新应用该规则的页面,我就会收到 500 - 内部服务器错误 .

我需要匹配所有情况,我已经尝试了几个小时的不同组合,但我遗漏了导致这些问题的原因。

在 .htaccess 上,我需要的组是 $2 和 $3,因为 $2 包含我在脚本中使用的 属性 ID,而 $3 是 URL 的所有其余部分与任何其他额外参数或查询字符串一样。

这么多不同配置的原因是用户可以设置自己的 URL 格式,但仅限于上面列出的选项。

这是请求 URL 时从 Apache 日志中提取的内容

https://localhost/en/property/rotterdam/apartment/lorem-ipsum-dolor-sit-amet/v-9/test/132?blah=123

日志

php74         | [Mon May 31 12:19:12.375537 2021] [rewrite:trace3] [pid 64] mod_rewrite.c(483): [client 172.18.0.1:63664] 172.18.0.1 - - [localhost/sid#7fd3d4ab1958][rid#7fd3d29d73c0/initial/redir#10] [perdir /var/www/html/] add per-dir prefix: en/properties/property/9/test/132&blah=123&blah=123&blah=123&blah=123&blah=123&blah=123&blah=123&blah=123&blah=123&blah=123&blah=123 -> /var/www/html/en/properties/property/9/test/132&blah=123&blah=123&blah=123&blah=123&blah=123&blah=123&blah=123&blah=123&blah=123&blah=123&blah=123

您可以试试这个重写规则:

RewriteEngine On

RewriteRule ^(en/)?property/(?:\S*?[-/v])?(\d+)(/.*)?$ properties/property/ [NC,L]

Updated RegEx Demo

我们在 -/v 字符后捕获 属性 ID。