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

WordPress下.htaccess实现特定页面www与非www流量分别重定向

特定页面分www/非www的重定向解决方案

你之前的规则无效是因为RewriteRule的匹配目标是URL的路径部分,不是完整的HTTP地址,要区分www和非www的主机名,得用RewriteCond来判断,再配合RewriteRule匹配页面路径。

把以下规则放在WordPress的代码块之前(也就是# BEGIN WordPress注释之前):

# 匹配www.example.com/help,重定向到example.com/contact
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^help$ http://example.com/contact [R=301,L]

# 匹配example.com/help,重定向到example.com/buy
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^help$ http://example.com/buy [R=301,L]

规则说明:

  • RewriteCond %{HTTP_HOST} ^www\.example\.com$:判断当前请求的主机名是www.example.com
  • RewriteRule ^help$ ...:匹配请求路径为/help的页面(^和$表示精确匹配,避免匹配/help/xxx这类子路径)
  • [R=301,L]:301是永久重定向,L表示这是最后一条规则,匹配后不再执行后续规则

如果你的站点用HTTPS,记得把http改成https。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 11:34:53