You need to enable JavaScript to run this app. .htaccess配置实现URL路径无论大小写均触发一致跳转规则
优惠活动
大模型
产品
解决方案
定价
更多

.htaccess配置实现URL路径无论大小写均触发一致跳转规则

问题根因

你遇到的现象是mod_speling模块和重写规则共同作用的结果:

  • 配置中开启了CheckSpelling On,该功能会自动纠正URL的大小写、近似拼写错误,当你访问/contact/pathname2时,模块识别到存在contact.php文件,会自动做跳转映射,没有走到错误逻辑;
  • 大写路径/Contact/pathname2没有触发拼写纠正规则,所以正常返回300错误页。

修改步骤

1. 调整mod_speling配置

优先关闭自动拼写纠正功能,避免模块自动映射近似存在的文件,如果你需要保留拼写检查仅关闭大小写纠正,可以将CheckSpelling On替换为:

CheckSpelling On
CheckCaseOnly On

如果完全不需要自动拼写纠正,直接关闭即可(更推荐该方案,避免非预期的自动跳转):

CheckSpelling Off

2. 优化重写规则

给原有页面重写规则增加存在性判断和终止匹配标记,确保仅当请求路径完全匹配规则、且无额外后缀时才映射到对应PHP文件,避免多余路径被错误匹配:
修改后完整的.htaccess配置如下:

RewriteEngine On
RewriteBase /

# HTTP跳转HTTPS
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.website.com/$1 [R=301,L]

# 关闭自动拼写纠正
CheckSpelling Off

# 仅当请求的文件/目录不存在时才走后续重写规则
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# 页面重定向规则
RewriteRule ^Home/?$ index.php [QSA,NC,L]
RewriteRule ^Inventory/?$ inventory.php [QSA,NC,L]
RewriteRule ^Shows/?$ shows.php [QSA,NC,L]
RewriteRule ^Contact/?$ contact.php [QSA,NC,L]

# SKU路径规则
RewriteRule ^Inventory/([^/]+)/?$ /vendors/pages/Inventory/LandingPage/DirectSKU.php?number=$1 [QSA,L,NC]

# 错误页配置
ErrorDocument 300 /vendors/pages/error.php
ErrorDocument 400 /vendors/pages/error.php
ErrorDocument 401 /vendors/pages/error.php
ErrorDocument 403 /vendors/pages/error.php
ErrorDocument 404 /vendors/pages/error.php
ErrorDocument 500 /vendors/pages/error.php

效果验证

修改后:

  • 无论是/Contact还是/contact路径,都会正常映射到contact.php页面
  • 无论是/Contact/pathname2还是/contact/pathname2,因为没有对应真实存在的文件,都会返回300错误页
  • Inventory下的SKU路径规则不受影响,正常生效

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.21 21:45:06