使用.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
相关产品推荐
相关产品推荐

