网站迁移后出现ERR_TOO_MANY_REDIRECTS循环问题求助
Why Your .htaccess Is Causing ERR_TOO_MANY_REDIRECTS
The redirect loop is definitely coming from the conflicting use of both RewriteRule and FallbackResource in your .htaccess file. Here's exactly why that happens:
Your RewriteRule already handles routing to index.php:
- The rule targets any request that isn't a directory, doesn't start with
/resource, and doesn't already includeindex.php. It rewrites those requests internally to/index.phpand stops processing further rewrite rules (thanks to the[L]flag).
- The rule targets any request that isn't a directory, doesn't start with
FallbackResource creates an unnecessary loop:
FallbackResource /index.phptells Apache to serveindex.phpwhenever the requested path doesn't exist. But since your RewriteRule already does this, having both leads to redundant processing.- Apache runs mod_rewrite first, which rewrites the request to
index.php. Then mod_dir (which handlesFallbackResource) kicks in. Even thoughindex.phpexists, the internal rewrite from mod_rewrite can trigger a secondary check where mod_dir tries to applyFallbackResourceagain. This creates an infinite internal loop that Apache eventually cuts off by sending theERR_TOO_MANY_REDIRECTSerror to the browser.
Subdirectory hosting can worsen this:
- If your site is in a subdirectory (not the server root), the absolute path
/index.phpin your RewriteRule points to the root'sindex.php(which probably doesn't exist). Apache then usesFallbackResourceto try serving your subdirectory'sindex.php, which starts the cycle over again.
- If your site is in a subdirectory (not the server root), the absolute path
The Fix
Remove the redundant FallbackResource line—your RewriteRule already covers the routing you need. Here's the cleaned-up .htaccess:
RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !\/resource.*$ RewriteRule ^((?!index\.php).+)$ /index.php [L]
If your site is hosted in a subdirectory, add a RewriteBase directive (replace /your-subdir/ with your actual subdirectory path) and adjust the RewriteRule target to use a relative path:
RewriteEngine on RewriteBase /your-subdir/ RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !\/resource.*$ RewriteRule ^((?!index\.php).+)$ index.php [L]
内容的提问来源于stack exchange,提问作者Elbarbu
相关产品推荐
相关产品推荐

