如何将所有WWW请求重定向至HTTPS非WWW地址?现有方案无效求指导
Fixing Your /about-us Redirect Issue
Hey Sam, let’s get this redirect sorted out—those mixed protocol and www variants of your /about-us page should all land on https://example.com/about-us without a hitch.
The problem with most generic www-to-non-www + HTTPS rules is they often handle the domain/protocol globally but don’t target the specific /about-us path, or they’re ordered incorrectly so other rules interfere. Here’s a targeted, tested solution for your .htaccess file:
RewriteEngine On # Redirect all 3 target URLs to https://example.com/about-us RewriteCond %{HTTP_HOST} ^www\.example\.com$ [OR] RewriteCond %{HTTPS} off RewriteRule ^about-us$ https://example.com/about-us [R=301,L]
Let’s break down what each part does:
RewriteEngine On: Turns on Apache’s URL rewriting module—this is mandatory for any rewrite rules to work.- First
RewriteCond: Matches requests coming to thewww.example.comsubdomain (covershttps://www.example.com/about-us). [OR]: Tells Apache to trigger the rule if either condition is true.- Second
RewriteCond: Matches requests using HTTP (covers bothhttp://www.example.com/about-usandhttp://example.com/about-us). RewriteRule: When the request path exactly matchesabout-us, it redirects to your desired HTTPS, non-www URL.R=301is a permanent redirect (great for SEO), andLtells Apache to stop processing further rules once this one runs.
Critical Notes to Make This Work:
- Place this rule first: If you have other rewrite rules in your
.htaccess, put this at the top—rules run top-to-bottom, and later rules might override this one. - Test with 302 first: Replace
R=301withR=302temporarily while testing. Browsers cache 301 redirects aggressively, so using a temporary redirect lets you tweak without clearing cache constantly. Switch back to 301 once you confirm it’s working. - Ensure mod_rewrite is enabled: Check with your hosting provider or server admin that Apache’s
mod_rewritemodule is turned on—this is required for.htaccessrewrite rules to function.
Give this a try, and you should see all three source URLs redirect cleanly to your target page.
内容的提问来源于stack exchange,提问作者Sam
相关产品推荐
相关产品推荐

