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

.htaccess中picture路径RewriteRule友好URL规则匹配错误,是什么原因?

错误原因

  • 重写规则缺少必要配置项
    当前目标规则RewriteRule ^picture/(.*)/(.*) picture.php?cid=$1&pid=$2存在两个核心配置缺失:
    • 没有添加正则结束锚点$:仅匹配URL前缀,只要请求路径以picture/xxx/xxx开头就会触发规则,不会校验后续是否还有其他内容
    • 没有添加[L]标记:重写后生成的新路径会再次进入重写流程循环匹配,Apache会尝试把picture/3/当做目录访问,自动拼接DirectoryIndex配置的index.php,最终生成picture/3/index.php的错误路径
  • 贪婪匹配符使用不合理
    (.*)为贪婪匹配规则,可匹配任意字符包括路径分隔符/,会尽可能多的吞入路径内容,且你需要匹配的分类ID、图片ID均为数字,用(.*)完全没有做格式校验,很容易匹配到非预期内容
  • 模板代码存在多余空格
    模板内href属性的条件判断前后加了多余空格,输出时会被带入最终的链接内容,可能触发服务端路径解析异常

修复方案

  1. 调整.htaccess重写规则,限定ID格式、补充锚点和L标记:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index$ index.php [L]
RewriteRule ^member/exit member.php?logout=1 [L]
# 修改后的picture规则
RewriteRule ^picture/([0-9]+)/([0-9]+)$ picture.php?cid=$1&pid=$2 [L]
RewriteRule ^user/([0-9]+)$ profile.php?usr=$1 [L]
#...
</IfModule>
  1. 去除模板链接中的多余空格:
<div>
    <a href="{if="$modewrite == 1"}picture/{$value.cat_id}/{$value.pic_id}{else}picture.php?cid={$value.cat_id}&amp;pid={$value.pic_id}{/if}" onmousemove="showBigPic(this,'showPicThumb',event);" onmouseout="mouseNoFollow();"><img src="../../{$thumbsFolder}/{$value.pic_name}" alt="{$value.pic_title}" title="{$value.pic_title}" /><span class="Imglayer"></span></a>
</div>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 14:06:02