apache mod_ewrite + regex将0附加到查询字符串参数

xesrikrc  于 2023-03-03  发布在  Apache
关注(0)|答案(1)|浏览(103)

我有这样的网址...

https://localhost/index.php?com=liste-cat-lieux&t=41&l=6088

我需要改写成这样:

https://localhost/index.php?com=liste-cat-lieux&t=41&l=06088

我尝试追加前导0

RewriteCond %{QUERY_STRING} ^(com=liste-cat-lieux&t=\d{2,3}&l=)(\d{4})$
RewriteRule ^index\.php /index?%10%2 [R=301,L]

当我用https://htaccess.madewithlove.com/之类的工具测试它时,它看起来工作得很好,但实际上它在htaccess或apache中并不工作

8i9zcol2

8i9zcol21#

您的重定向规则看起来正确,但有一个问题。它不检查现有前导零,这导致012300123等等。
我建议您保留您的重定向规则为:

RewriteCond %{QUERY_STRING} ^(com=liste-cat-lieux&t=\d{2,3}&l=)([1-9]\d{3})$ [NC]
RewriteRule ^index\.php /$0?%10%2 [R=301,L,NC,NE]

这里,模式[1-9]\d*匹配参数值l,该参数值以数字1到9开头,后跟0或更多数字。
另请注意,在重定向URL中使用/$0,保持与RewriteRule相同的匹配URI,即/index.php

相关问题