.htaccess添加Part2后引发无限重定向,请求排查配置错误
Hey there, let's troubleshoot that infinite redirect loop you're hitting when adding Part 2 to your .htaccess file. From what you shared, your site works perfectly with just Part 1, but adding the second set of rules breaks everything—let's get to the bottom of this.
First, Let's Clarify Your Current Setup
I'll start by filling in the likely full version of your Part 1 config (since you had a truncated RewriteRule):
# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://www.mydomainname.com/$1 [R=301,L] # ... followed by WordPress's default rewrite rules </IfModule> # END WordPress
Why the Redirect Loop Happens
The most common culprit here is conflicting rules between Part 1 and Part 2. For example, if Part 2 includes a rule to force www. without checking if the connection is already HTTPS, or if it repeats the HTTPS redirect but uses inconsistent logic, your server gets stuck bouncing between states.
Another frequent issue is using %{SERVER_PORT} 80 to detect HTTP traffic—this can fail if your site is behind a CDN or reverse proxy (many use non-standard ports for internal HTTPS connections), causing the rule to incorrectly trigger even on HTTPS requests.
Fixed .htaccess Configuration
Here's a combined, conflict-free config that enforces HTTPS and your preferred www. hostname without loops:
# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On # 1. Redirect ALL HTTP traffic to HTTPS (more reliable than checking port 80) RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://www.mydomainname.com/$1 [R=301,L] # 2. Optional: Enforce www. (uncomment if you need to redirect non-www HTTPS to www) # RewriteCond %{HTTP_HOST} !^www\.mydomainname\.com$ # RewriteRule ^(.*)$ https://www.mydomainname.com/$1 [R=301,L] # WordPress's default rewrite rules (leave these intact) RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress
Key Fixes Explained
- Use
%{HTTPS} offinstead of%{SERVER_PORT} 80: This directly checks if the connection is secure, avoiding issues with proxies/CDNs that use non-standard ports. - Order your rules correctly: Always handle HTTPS first, then the
www.redirect. This ensures users are sent to the final URL in one step, not multiple redirects. - Add a condition for hostname checks: If you uncomment the
www.enforcement rule, theRewriteCondensures we only redirect if the user isn't already onwww.mydomainname.com—no more loops!
Additional Troubleshooting Steps
- Clear your browser cache: 301 redirects are cached aggressively. Test in incognito mode or clear your cache to see the new config take effect.
- Verify WordPress settings: Go to
Settings > Generalin your WP admin and confirm bothWordPress Address (URL)andSite Address (URL)are set tohttps://www.mydomainname.com. Mismatched URLs can cause loops even with correct .htaccess rules. - Check your SSL certificate: Make sure your cert covers both
mydomainname.comandwww.mydomainname.com(a wildcard or SAN certificate works). A missing cert for one hostname can break redirects.
内容的提问来源于stack exchange,提问作者Mark Anderson

