.htaccess的RewriteRule规则返回404错误求助
解决.htaccess RewriteRule返回404的问题
你的规则出现404,主要是路径匹配逻辑和正则写法的问题,按以下步骤修改:
添加RewriteBase指定目录
在RewriteEngine On后添加RewriteBase /df-talents/,确保重写规则基于当前子目录路径匹配,避免服务器解析时的路径混乱。修正静态文件排除规则的正则
原规则的正则未匹配后缀结尾,可能误排除或漏排除文件,改成带$的精准匹配,同时加NC忽略大小写:RewriteCond %{REQUEST_URI} !\.(html|js|css)$ [NC] RewriteCond %{REQUEST_URI} !\.(jpg|jpeg|png|gif|txt|ico|webp)$ [NC] RewriteCond %{REQUEST_URI} !\.(eot|ttf|woff)$ [NC]调整RewriteRule匹配逻辑
- 如果需要内部转发(地址栏URL不变,服务器返回index.html内容),用这条规则:
RewriteRule ^.*classes.*$ index.html [NC,L] - 如果需要外部重定向(地址栏URL直接变为/df-talents/index.html),添加
R=302标记(临时重定向,永久重定向用R=301):RewriteRule ^.*classes.*$ /df-talents/index.html [NC,L,R=302]
- 如果需要内部转发(地址栏URL不变,服务器返回index.html内容),用这条规则:
完整的.htaccess示例(内部转发版本)
RewriteEngine On RewriteBase /df-talents/ RewriteCond %{REQUEST_URI} !\.(html|js|css)$ [NC] RewriteCond %{REQUEST_URI} !\.(jpg|jpeg|png|gif|txt|ico|webp)$ [NC] RewriteCond %{REQUEST_URI} !\.(eot|ttf|woff)$ [NC] RewriteRule ^.*classes.*$ index.html [NC,L]
额外检查
确认服务器配置中,/df-talents目录的AllowOverride参数已设置为All,否则.htaccess文件的规则不会被服务器解析。
内容的提问来源于stack exchange,提问作者Keroster
相关产品推荐
相关产品推荐

