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

使用.htaccess移除页面URL的.php后缀及index.php的方法咨询

实现方案

你只需在现有规则的RewriteEngine On语句后,新增专门的index移除规则即可,注意规则优先级要高于原有.php后缀移除、博客路由规则,避免逻辑冲突。

新增规则逻辑说明

  • 匹配所有带index或index.php的请求路径,返回301永久重定向到剔除index后的路径,保证SEO权重不会分散
  • 增加重写条件排除实际存在的文件/目录,避免误杀正常业务请求

修改后完整的.htaccess代码

<IfModule mod_rewrite.c>
    RewriteEngine On

    # 新增:移除URL中的index/index.php
    RewriteCond %{THE_REQUEST} /index(\.php)?[\s/?] [NC]
    RewriteRule ^(.*?)index(\.php)?$ /$1 [L,R=301,NC]

    #removing extension from url
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ $1.php [NC,L]
    #blog
    #if the file with the specified name in the browser doesn’t exist, or the directory in the browser doesn’t exist then procede to the rewrite rule below
    RewriteCond %{REQUEST_FILENAME} !-d [NC]
    RewriteCond %{REQUEST_FILENAME} !-f [NC]
    RewriteRule ^(.*)$ blog_detail.php?id=$1 [QSA,L]   
</IfModule>

验证注意事项

配置修改完成后建议清理浏览器缓存后再测试,避免浏览器缓存的旧301规则影响效果验证。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 05:54:04