Apache子目录部署应用RewriteRule引发无限循环问题排查
问题原因与解决方法
你的Apache出现AH00124错误是因为重写规则触发了无限循环重定向。当请求被重写到index.php后,Apache会重新处理.htaccess规则,而index.php的请求又满足规则中的!-f/!-d条件(要么是服务器无法正确识别index.php为存在的文件,要么是[L]标志仅终止当前轮重写而非整个流程),导致反复重写超过10次触发限制。
修复方案
根据你的Apache版本选择对应修改:
方案1:Apache 2.4及以上(推荐)
将RewriteRule的[L]替换为[END],[END]标志会直接终止所有重写处理,避免再次触发规则:
<IfModule mod_rewrite.c> RewriteEngine on RewriteBase /cms RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*) index.php [END] </IfModule>
方案2:Apache 2.2及以下
添加额外的RewriteCond排除对index.php的请求,避免循环:
<IfModule mod_rewrite.c> RewriteEngine on RewriteBase /cms RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !^/cms/index.php$ RewriteRule ^(.*) index.php [L] </IfModule>
额外检查
同时确认应用文件结构与请求转发逻辑匹配:如果CloudFront将/cms/*的请求转发到Apache,需确保index.php的物理路径与重写后的路径对应——若应用根目录是Apache的DocumentRoot,则可能需要调整RewriteBase为/,并修改规则为:
<IfModule mod_rewrite.c> RewriteEngine on RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^cms/(.*) index.php [L] </IfModule>
内容的提问来源于stack exchange,提问作者Adrian Varysoo
相关产品推荐
相关产品推荐

