.htaccess文件引发300 Multiple Choices错误及页面自重定向问题求助
Hey there, let's tackle this frustrating .htaccess issue head-on. Those 300 Multiple Choices errors and unexpected self-redirects are almost always tied to conflicting rewrite rules or ambiguous resource paths—especially since your file was handling custom error pages and .php extension removal before. Let's break down the fixes step by step.
First, Confirm the .htaccess Is the Culprit
Start by isolating the problem:
- Rename your current
.htaccessfile to something like.htaccess.bak(this disables it temporarily). - Visit your site: if the 300 errors disappear (even if .php extensions come back), you know the issue is definitely in this file.
Common Causes & Fixes
1. File vs. Directory Name Conflicts
The 300 Multiple Choices error often pops up when you have a file and directory with the same name (e.g., /about.php and /about/). When you visit /about, the server can't decide whether to load the file or the directory index, so it throws the error.
Fix:
Either rename the directory or the file to avoid the conflict. If you want to keep both, add a rule to prioritize files over directories in your .htaccess:
# Prioritize files over directories to avoid 300 errors DirectoryIndex index.php RewriteCond %{REQUEST_FILENAME} -f RewriteRule ^ - [L]
2. Rewrite Rule Loops or Misordering
Your extension-removal rules might be creating loops, or the order of rules (custom error pages vs. rewrites) could be causing conflicts. Here's a cleaned-up, conflict-free .htaccess that handles both your original goals:
# Enable rewrite engine RewriteEngine On # -------------------------- # Custom Error Pages (set first to avoid rewrite interference) # -------------------------- ErrorDocument 404 /404.php ErrorDocument 500 /500.php # Add other error codes as needed (e.g., 403, 401) # -------------------------- # Remove .php Extension & Prevent Loops # -------------------------- # Redirect users from .php URLs to clean URLs (301 permanent redirect) RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC] RewriteRule ^ %1 [R=301,L] # Rewrite clean URLs to their corresponding .php files (internal rewrite) # Skip if the request is for a real directory RewriteCond %{REQUEST_FILENAME} !-d # Only rewrite if the .php file actually exists RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*)$ $1.php [L]
Why this works:
- We set custom error pages first so rewrites don't interfere with their paths.
- The redirect rule handles users who might type in the .php URL directly, sending them to the clean version.
- The internal rewrite rule only triggers if:
- The requested path isn't a real directory (avoids directory/file conflicts).
- The matching .php file exists (prevents invalid rewrites that could cause errors).
Post-Fix Validation
After updating your .htaccess:
- Clear your browser cache (old redirects might be cached).
- Test a clean URL (e.g.,
/about)—it should load without errors or loops. - Test a .php URL (e.g.,
/about.php)—it should redirect to/about. - Test a non-existent page—your custom 404 page should load.
If Issues Persist
- Check for nested .htaccess files in subdirectories—they might inherit or override your main rules.
- If you have server access, enable rewrite logging to debug rule execution:
This log will show exactly how each request is being processed, making it easier to spot hidden conflicts.RewriteLog "/var/log/apache2/rewrite.log" RewriteLogLevel 3
内容的提问来源于stack exchange,提问作者Shoxt3r

