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

如何通过.htaccess允许Stripe Webhook访问并解决301错误

解决Stripe Webhook返回301错误的.htaccess配置方案

问题原因

你的.htaccess规则中,原本对所有带.php后缀的请求(包括POST请求)都执行301重定向到无后缀URL。而Stripe发送Webhook通知时,是直接请求你的Webhook PHP文件(比如webhook.php),触发了这条重定向规则,导致返回301错误。Stripe的Webhook通知不支持跟随这类重定向,最终导致验证失败。

修改后的.htaccess配置

Options +FollowSymLinks -MultiViews
RewriteEngine On

RewriteBase /

# 强制HTTPS并移除www前缀
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

# 排除Stripe Webhook的PHP文件,避免被重定向
RewriteCond %{REQUEST_URI} !^/webhook\.php$ [NC]
# 仅对GET请求移除.php后缀(POST请求不处理)
RewriteCond %{THE_REQUEST} ^GET\ /.*\.php\ HTTP.*$ [NC]
RewriteRule ^(.*)\.php$ $1 [R=301,L]

# 重写无后缀URL到对应的.php文件
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule (.+) $1.php [L]

# 激活和重置链接的重写规则
RewriteRule ^(activate)/([a-f0-9]+)$ $1.php/$2 [L]
RewriteRule ^(reset)/([a-f0-9]+)$ $1.php/$2 [L]

# Stripe Webhook IP白名单
Order allow,deny
Allow from 3.18.12.63
Allow from 3.130.192.231
Allow from 13.235.14.237
Allow from 13.235.122.149
Allow from 18.211.135.69
Allow from 35.154.171.200
Allow from 52.15.183.38
Allow from 54.88.130.119
Allow from 54.88.130.237
Allow from 54.187.174.169
Allow from 54.187.205.235
Allow from 54.187.216.72
satisfy any

关键修改点

  • 限制.php后缀重定向仅针对GET请求:将原规则中匹配GET|POST的条件改为仅匹配GET,Stripe的Webhook是POST请求,不会触发这条301重定向
  • 排除Webhook文件:添加RewriteCond %{REQUEST_URI} !^/webhook\.php$ [NC]规则,确保你的Webhook PHP文件不会被重定向,若你的Webhook文件名不是webhook.php,替换成实际文件名即可
  • 其他原有规则保持不变,不影响网站的HTTPS强制、无后缀URL访问和IP白名单功能

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 03:12:08