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

将所有页面重定向至同目录新子目录的故障排查

问题分析与解决

问题场景

手动将网站迁移至新主机,域名等信息保持不变,根目录保留旧站点,新WordPress站点安装在subfolder子目录。配置根目录.htaccess后,首页可正常重定向到新站点,但其他页面显示旧站内容并返回404错误。

根目录原.htaccess配置

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.org$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.org$
RewriteRule ^(/)?$ subfolder/index.php [L] 
</IfModule>

子目录subfolder内.htaccess配置(完整WordPress迁移包自带)

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Redirs Base Redirect:
Redirect 301 /redirs https://example.com/redirs-target

# Redirects with sources which do NOT contain a query string:
RedirectMatch 301 ^/parents-of-rsym/?$ https://example.org/youth/parents-of-river/
RedirectMatch 301 ^/index.php/mens-page/?$ https://example.org/mens-ministry/
RedirectMatch 301 ^/index.php/about/what-to-expect/?$ https://example.org/what-to-expect/
RedirectMatch 301 ^/index.php/about/staff-pr/?$ https://example.org/staff/
RedirectMatch 301 ^/index.php/about/statement-of-faith/?$ https://example.org/we-believe/
RedirectMatch 301 ^/index.php/about/contact-link/?$ https://example.org/contact/
RedirectMatch 301 ^/index.php/youth-page/?$ https://example.org/youth-group-ministry/
RedirectMatch 301 ^/index.php/womens-page/womens-ministry-blog/?$ https://example.org/womens-ministry-blog/
RedirectMatch 301 ^/index.php/resources/calendar2/?$ https://example.org/calendar/
RedirectMatch 301 ^/index.php/womens-page/?$ https://example.org/womens-ministry/
RedirectMatch 301 ^/index.php/96-recipes/?$ https://example.org/womens-ministry/
RedirectMatch 301 ^/?$ https://example.org/youth-group-ministry/
RedirectMatch 301 ^/index.php/programs/youth-page-2/youth-program-info-page/?$ https://example.org/youth-group-ministry/
RedirectMatch 301 ^/index.php/video/?$ https://example.com/online-church
RedirectMatch 301 ^/index.php/special-services/apologetics-forum-of-snohomish-county-afsc/?$ https://example.com/external-forum
# 以下省略大量同域名跳转规则...

# BEGIN 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>
# END WordPress

问题原因

  1. 根目录.htaccess规则逻辑错误:

    • 首页重定向规则排在通用规则之后,且通用规则未添加[L]标记,导致请求处理流程混乱,非首页请求可能被旧站点规则拦截。
    • 未确保转发到子目录的请求直接终止处理,后续规则干扰了新站点的路径解析。
  2. 子目录.htaccess配置不匹配:

    • RewriteBase / 设置错误,WordPress安装在子目录,必须将其改为RewriteBase /subfolder/,否则系统会错误地以根目录为基准解析路径,导致页面404。
    • 子目录内包含大量基于根目录的RedirectMatch规则,这些跳转逻辑应该放在根目录.htaccess中,否则会与根目录的重写规则冲突,打乱请求流向。

解决方案

1. 修正根目录.htaccess

调整规则顺序,给通用重写规则添加[L]标记,确保请求正确转发到子目录后终止处理:

<IfModule mod_rewrite.c>
RewriteEngine on

# 优先处理首页请求,直接转发到子目录首页
RewriteCond %{HTTP_HOST} ^(www.)?example.org$
RewriteRule ^(/)?$ subfolder/index.php [L]

# 转发所有非文件、非目录的请求到子目录
RewriteCond %{HTTP_HOST} ^(www.)?example.org$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1 [L]
</IfModule>

2. 修正子目录subfolder/.htaccess

  • 调整RewriteBase:将RewriteBase /修改为RewriteBase /subfolder/
  • 迁移跳转规则:将所有Redirect和RedirectMatch规则移到根目录.htaccess中,避免子目录规则干扰新站点路径解析;若暂时不需要跳转,可先注释这些规则。
  • 修正WordPress重写规则:确保WordPress的内部重写规则指向子目录的index.php

修改后的子目录.htaccess核心内容:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /subfolder/

# 注释或迁移所有跳转规则到根目录
# Redirect 301 /redirs https://example.com/redirs-target
# RedirectMatch 301 ^/parents-of-rsym/?$ https://example.org/youth/parents-of-river/
# ...

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /subfolder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /subfolder/index.php [L]
</IfModule>
# END WordPress

3. WordPress后台配置检查

登录新站点后台,进入设置→常规:

  • 将「站点地址(URL)」设置为https://example.org(实现根域名访问新站点)
  • 将「WordPress地址(URL)」设置为https://example.org/subfolder(指向实际安装路径)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 18:20:48