使用 .htaccess 删除图像的可变部分 URL

Remove a variable part of an image URL using .htaccess

我有一张图片可以通过以下方式访问 URL:

https://localhost/hta/-7524-34-B.webp

我需要能够像下面这样使用 URL 访问相同的图像并删除 <something> 部分:

https://localhost/hta/<something>-7524-34-B.webp

如何使用 .htaccess 执行此操作?

更新:

我添加了以下内容.htaccess

RewriteEngine On
RewriteRule ^.*?(?=&). /hta/ [NC,L]

当我写以下内容时 URL 它没有打开图像。仅列出 hta 目录。

http://localhost/hta/something-else&-7524-34-B.webp

要在内部将 /hta/<something>-7524-34-B.webp 形式的 URL 重写为 /hta/-7524-34-B.webp(删除 <something>),您可以在根 .htaccess 文件:

RewriteEngine On
RewriteRule ^hta/[^/]+(-7524-34-B\.webp)$ hta/ [L]

正则表达式 [^/]+ 匹配 <something>,它被丢弃。 URL-路径 -7524-34-B\.webp 的剩余部分被捕获,并在 substitution 字符串(第二个参数)中引用 反向引用。