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

使用.htaccess重写API URL并移除查询参数(含错误解决)

URL重写与查询参数移除(.htaccess配置)

当前网站根目录.htaccess配置

ErrorDocument 404 /404.html

# 移除PHP扩展名
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

# 移除HTML扩展名
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.html -f 
RewriteRule ^(.*)$ $1.html

# 移除id查询参数(原尝试规则)
RewriteEngine on 
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^webhook/(.*)$ webhook.php?q=$1

需求目标

将 https://example.com/api/webhook.php?id=10 重写为 https://example.com/api/webhook/10,同时确保webhook.php能正确获取id参数值。

问题排查

在webhook.php中添加测试代码:

<?php echo $_GET['id']; ?>

访问原URL时页面正常输出10,但访问重写后的URL出现500内部服务器错误,服务器日志提示:

Request exceeded the limit of 4 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace

推测是重写规则触发无限循环,达到内部重定向次数上限。

解决方案

添加以下规则到.htaccess文件中,问题即可修复:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule   ^api/webhook/(.+)$   api/webhook.php?id=$1   [L]

现在访问https://example.com/api/webhook/10可正常加载webhook.php,页面输出10,参数传递正常。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 18:39:20