You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.htaccess正则URL重写问题:含圆点的URL参数解析错误

问题描述

需要将以下URL重定向:

http://localhost/iiif/http/image-service/papyrus,1/full/400,300/0/default.jpg

目标URL为:

http://localhost/iiif/http/image.php?req=763648&id=papyrus,1&region=full&size=400,300&rotation=0&quality=default&format=jpg

现有重写规则:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /iiif/http/    
RewriteRule image-service/(.*)/(.*)/(.*)/(.*)/(.*).(.*)$ image.php?req=763648&id=$1&region=$2&size=$3&rotation=$4&quality=$5&format=$6 [R=301,NC,L,P]
</IfModule>

实际返回的参数结果:

Array
(
    [req] => 763648
    [id] => papyrus,1
    [region] => full
    [size] => 400,300
    [rotation] => 0
    [quality] => default.jp.
)

预期的参数结果:

Array
(
    [req] => 763648
    [id] => papyrus,1
    [region] => full
    [size] => 400,300
    [rotation] => 0
    [quality] => default
    [format] => jpg
)

问题源于URL中的句点导致正则匹配异常,需修正重写规则。

修正方案

核心问题是正则里的(.*)属于贪婪匹配,会尽可能捕获更多字符(包括句点)。原规则中(.*).(.*)$的第一个(.*)会把default.jp全部捕获,剩下的.被第二个(.*)获取,最终导致quality参数值错误。

修改后的重写规则:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /iiif/http/    
RewriteRule image-service/(.*)/(.*)/(.*)/(.*)/([^.]+)\.([^.]+)$ image.php?req=763648&id=$1&region=$2&size=$3&rotation=$4&quality=$5&format=$6 [R=301,NC,L,P]
</IfModule>

规则说明

  • [^.]+表示匹配除句点外的任意字符,能精准将default和jpg分别捕获到$5(quality参数)和$6(format参数)。
  • 用([^.]+)\.([^.]+)替代原规则的(.*).(.*),明确分隔quality和format部分,彻底避免贪婪匹配带来的错误。

内容的提问来源于stack exchange,提问作者sariDon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 00:20:39