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

如何在.htaccess中避免410错误码触发跳转返回200状态码

.htaccess配置410规则跨协议/域名前缀不生效问题解决方案

问题根因

  • 410规则未添加L终止标志:所有携带G(返回410)的重写规则都没有加L标志,匹配到410规则后后续的301重定向规则仍会执行,直接覆盖410状态码
  • 重定向规则未排除错误文档:你配置了ErrorDocument 410 /410.php,触发410时Apache会内部跳转加载410.php,这个内部请求会被后续的非www转www、HTTP转HTTPS规则捕获,触发301外部跳转,导致最终返回200状态码
  • 配置存在笔误:HTTPS跳转规则的排除条件重复写了两次/folder1/漏掉了folder2,非www跳转的目标地址少了.com后缀

修复步骤

  1. 给所有410重写规则添加L标志
    把所有类似RewriteRule .* - [G,NC]的规则修改为RewriteRule .* - [G,NC,L],覆盖你写的三组410重写规则。
  2. 修正非www转www规则
    原规则:
#Redirect to www except folder1 and forder2
RewriteCond %{HTTP_HOST} !^(www|videos|css|js|images)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI}  !(/folder1/|/folder2/) [NC]
RewriteRule ^(.*) https://www.example/$1 [L,R=301]

修正后:

#Redirect to www except folder1 and folder2, exclude error documents
RewriteCond %{HTTP_HOST} !^(www|videos|css|js|images)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI}  !(/folder1/|/folder2/|/410\.php|/404\.php) [NC]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*) https://www.example.com/$1 [L,R=301]
  1. 修正HTTP转HTTPS规则
    原规则:
#Redirect to https except folder1 and folder2
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !(folder1|folder2)/([0-9]+)-g([0-9]+)/ [NC]
# Include in the next line all folders to exclude
RewriteCond %{REQUEST_URI} !(/folder1/|/folder1/)(.*) [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

修正后:

#Redirect to https except folder1 and folder2, exclude error documents
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !(folder1|folder2)/([0-9]+)-g([0-9]+)/ [NC]
RewriteCond %{REQUEST_URI} !(/folder1/|/folder2/|/410\.php|/404\.php)(.*) [NC]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  1. 补充410.php状态码配置
    在你的/410.php文件最顶部添加以下代码,确保无论什么场景访问都返回410状态码:
<?php
http_response_code(410);
// 原有页面内容保持不变
?>

验证方法

修改完成后清理浏览器缓存,分别访问四个测试场景的URL,都应该直接返回410状态码,不会出现301跳转。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 19:45:04