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

.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} off instead 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, the RewriteCond ensures we only redirect if the user isn't already on www.mydomainname.com—no more loops!

Additional Troubleshooting Steps

  1. Clear your browser cache: 301 redirects are cached aggressively. Test in incognito mode or clear your cache to see the new config take effect.
  2. Verify WordPress settings: Go to Settings > General in your WP admin and confirm both WordPress Address (URL) and Site Address (URL) are set to https://www.mydomainname.com. Mismatched URLs can cause loops even with correct .htaccess rules.
  3. Check your SSL certificate: Make sure your cert covers both mydomainname.com and www.mydomainname.com (a wildcard or SAN certificate works). A missing cert for one hostname can break redirects.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:05:02