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

.htaccess RewriteRule出现404错误且无法隐藏index.php

解决URL短链接服务的.htaccess配置问题

针对你遇到的短链接404、根目录访问显示index.php这两个问题,可修改.htaccess配置如下:

DirectoryIndex index.php

RewriteEngine On

# 处理6位字符的短链接转发
RewriteRule ^([a-zA-Z0-9]{6})$ index.php?redirect=$1 [L]

# 隐藏index.php,直接访问index.php时重定向到根目录
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\s [NC]
RewriteRule ^index\.php$ / [R=301,L]

# 处理其他非文件/目录的请求转发
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

配置说明

  • DirectoryIndex index.php:设置服务器默认将根目录请求指向index.php,访问example.com/时地址栏不会显示index.php。
  • 第一条规则:精准匹配6位字母数字的短链接路径,直接转发到index.php?redirect=xxx,[L]确保匹配后不再执行后续规则,避免冲突。
  • 新增的重定向规则:当用户直接访问example.com/index.php时,通过301永久重定向到根目录,强制地址栏隐藏文件名。
  • 最后一组规则:保留原有的非文件/目录请求转发逻辑,适配其他业务需求。

注意事项

  • 确保服务器已启用mod_rewrite模块。
  • 网站根目录的Apache配置中,AllowOverride需设置为All,确保.htaccess规则生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 16:45:33