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

修复WordPress站点HTTP转HTTPS子页面错误301重定向问题

问题:IONOS托管WordPress站点HTTP子页面重定向错误

主域名http://example.com可正常重定向至https://example.com,但子页面http://example.com/subpage错误跳转至https://example.com/,而非对应HTTPS子页面,导致Google Search Console检测到索引重定向错误。

环境信息

  • WordPress版本:最新版
  • 已安装插件:
    • Advanced Import
    • Duplicate Page
    • Elementor Pro
    • IONOS Assistant(已停用)
    • IONOS Help(已停用)
    • IONOS Journey(已停用)
    • IONOS Login
    • IONOS Loop(已停用)
    • IONOS Navigation
    • IONOS Performance
    • IONOS Security
    • Music Player for Elementor
    • Title Remover
    • WPvivid Backup Plugin
    • Yoast SEO Premium
  • 固定链接设置:https://example.com/%category/%postname%/

当前.htaccess相关代码段

HTTP转HTTPS重定向规则

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(/(.*))?$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>

IONOS Performance缓存规则

<IfModule mod_rewrite.c>
    # ENGINE ON
    RewriteEngine on

    # set hostname directory
    RewriteCond %{HTTPS} on
    RewriteRule .* - [E=IONOS_PERFORMANCE_HOST:https-%{HTTP_HOST}]

    RewriteCond %{HTTPS} off
    RewriteRule .* - [E=IONOS_PERFORMANCE_HOST:%{HTTP_HOST}]

    # set subdirectory
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_METHOD} GET
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteCond %{REQUEST_FILENAME} !.(gif|jpg|png|jpeg|css|xml|txt|js|php|scss|webp|mp3|avi|wav|mp4|mov)$ [NC]
    RewriteRule .* - [E=IONOS_PERFORMANCE_DIR:%{REQUEST_URI}/]

    RewriteCond %{REQUEST_URI} /$
    RewriteRule .* - [E=IONOS_PERFORMANCE_DIR:%{REQUEST_URI}]

    RewriteCond %{REQUEST_URI} ^$
    RewriteRule .* - [E=IONOS_PERFORMANCE_DIR:/]

    # gzip
    RewriteRule .* - [E=IONOS_PERFORMANCE_SUFFIX:]
    <IfModule mod_mime.c>
        RewriteCond %{HTTP:Accept-Encoding} gzip
        RewriteRule .* - [E=IONOS_PERFORMANCE_SUFFIX:.gz]
        AddType text/html .gz
        AddEncoding gzip .gz
    </IfModule>

    # Main Rules
    RewriteCond %{HTTP_ACCEPT} .*text/html.*
    RewriteCond %{ENV:INITIAL_REQUEST_METHOD} ^$
    RewriteCond %{ENV:REDIRECT_INITIAL_REQUEST_METHOD} ^$
    RewriteCond %{QUERY_STRING} ^$
    RewriteCond %{REQUEST_URI} !^/(wp-admin|wp-content/cache)/.*
    RewriteCond %{HTTP_COOKIE} !(wp-postpass|wordpress_logged_in|comment_author)_
    RewriteCond /homepages/9/d4297862886/htdocs/./wordpress/wp-content/cache/ionos-performance/%{ENV:IONOS_PERFORMANCE_HOST}%{ENV:IONOS_PERFORMANCE_DIR}index.html%{ENV:IONOS_PERFORMANCE_SUFFIX} -f
    RewriteRule ^(.*) /wp-content/cache/ionos-performance/%{ENV:IONOS_PERFORMANCE_HOST}%{ENV:IONOS_PERFORMANCE_DIR}index.html%{ENV:IONOS_PERFORMANCE_SUFFIX} [L]
</IfModule>

WordPress默认重写规则

<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>

修复方案

问题根源在于HTTP转HTTPS的重定向规则:原规则的正则表达式^(/(.*))?$试图捕获带前导斜杠的路径,但在RewriteBase /的配置下,RewriteRule的匹配对象本身不包含前导斜杠,导致$1变量为空,最终重定向到根域名。

修改重定向规则

替换原HTTP转HTTPS规则为以下代码:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTPS} !=on
    RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>

关键优化点

  1. 用%{HTTPS} !=on替代%{SERVER_PORT} 80,更准确适配非标准端口的HTTPS配置
  2. 正则表达式^(.*)$直接匹配请求路径(不含前导斜杠),确保$1正确捕获子页面完整路径
  3. 保留R=301永久重定向标记和L终止后续规则的标记,避免规则冲突

后续验证步骤

  1. 清除IONOS Performance缓存:进入WordPress后台的IONOS Performance插件设置,手动清除缓存,避免旧缓存导致异常
  2. 测试重定向:访问http://example.com/subpage,确认跳转至https://example.com/subpage;或用curl命令验证:
    curl -I http://example.com/subpage
    
    查看响应头的Location字段是否正确
  3. 更新Google Search Console:提交重定向后的HTTPS子页面,请求重新索引

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 13:38:16