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

非www域名重定向至www时子目录及内页404问题求助

解决非www域名重定向至www的问题

问题根源

你的重定向规则放在了WordPress核心重写规则之后,而WordPress规则中的[L]标记会终止后续Rewrite规则的执行。当访问子目录/内页时,请求先被WordPress的规则接管,根本没触发你的非www转www规则,导致404错误。

修复方案

把非www转www的重定向规则放在所有规则最顶部(WP Rocket规则之前),同时统一使用HTTPS,避免混合内容问题。

修改后的完整.htaccess内容

# 非www转www重定向(HTTPS)
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# example.in 转 www.example.com 重定向
RewriteCond %{HTTP_HOST} ^example\.in [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

# BEGIN WP Rocket v3.14.4.2
# END WP Rocket

Options -Indexes

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress example.com is for reference

<IfModule mod_substitute.c>
SubstituteMaxLineLength 10M
</IfModule>
# BEGIN DS-XML-RPC-API
# The directives (lines) between "BEGIN DS-XML-RPC-API" and "END DS-XML-RPC-API" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<Files xmlrpc.php>
order deny,allow
deny from all

</Files>

# END DS-XML-RPC-API


# Admin account username disclosure

RewriteCond %{REQUEST_URI} !^/wp-admin [NC]
RewriteCond %{QUERY_STRING} author=\d
RewriteRule ^ /? [L,R=301]

# Wordfence WAF
<IfModule mod_php5.c>
    php_value auto_prepend_file '/var/www/html/wordfence-waf.php'
</IfModule>
<IfModule mod_php7.c>
    php_value auto_prepend_file '/var/www/html/wordfence-waf.php'
</IfModule>
<IfModule mod_php.c>
    php_value auto_prepend_file '/var/www/html/wordfence-waf.php'
</IfModule>
<Files ".user.ini">
<IfModule mod_authz_core.c>
    Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
    Order deny,allow
    Deny from all
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
</IfModule>
</Files>





# END Wordfence WAF

# BEGIN ShortPixel Adaptive Images
# The directives (lines) between "BEGIN ShortPixel Adaptive Images" and "END ShortPixel Adaptive Images" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.

# END ShortPixel Adaptive Images

ServerSignature Off



# BEGIN Headers Security Advanced & HSTS WP 5.0.28
<IfModule mod_headers.c>
Header set Access-Control-Allow-Methods "GET,POST"
Header set Access-Control-Allow-Headers "Content-Type, Authorization"
Header set Content-Security-Policy "upgrade-insecure-requests;"
Header set Cross-Origin-Embedder-Policy "unsafe-none; report-to='default'"
Header set Cross-Origin-Embedder-Policy-Report-Only "unsafe-none; report-to='default'"
Header set Cross-Origin-Opener-Policy "unsafe-none"
Header set Cross-Origin-Opener-Policy-Report-Only "unsafe-none; report-to='default'"
Header set Cross-Origin-Resource-Policy "cross-origin"
Header set Permissions-Policy "accelerometer=(), autoplay=(), camera=(), cross-origin-isolated=(), display-capture=(self), encrypted-media=(), fullscreen=*, geolocation=(self), gyroscope=(), keyboard-map=(), magnetometer=(), microphone=(), midi=(), payment=*, picture-in-picture=(), publickey-credentials-get=(), screen-wake-lock=(), sync-xhr=(), usb=(), xr-spatial-tracking=(), gamepad=(), serial=()"
Header set Referrer-Policy "strict-origin-when-cross-origin"
Header set Strict-Transport-Security "max-age=63072000"
Header set X-Content-Security-Policy "default-src 'self'; img-src *; media-src * data:;"
Header set X-Content-Type-Options "nosniff"
Header set X-Frame-Options "SAMEORIGIN"
Header set X-XSS-Protection "1; mode=block"
Header set X-Permitted-Cross-Domain-Policies "none"
</IfModule>
# END Headers Security Advanced & HSTS WP

关键说明

  • 重定向规则必须放在最前面,确保所有请求先经过重定向判断,再进入WordPress或其他插件的规则处理。
  • 使用%{REQUEST_URI}替代$1,能更可靠地保留完整的请求路径,避免因RewriteBase设置导致的路径丢失问题。
  • 统一使用HTTPS,和你现有HSTS配置保持一致,避免浏览器安全警告。
  • 301是永久重定向,浏览器会缓存该规则,测试前建议清除浏览器缓存或使用无痕模式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 17:43:17