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

WordPress中.htaccess域名重定向排除指定文件夹方法

基于语言的多域名重定向排除指定文件夹的修复方案

我们在WordPress的.htaccess文件中配置了基于语言的多域名重定向规则,规则如下:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.es$
RewriteRule (.*)$ https://www.example.com/es/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.example.de$
RewriteRule (.*)$ https://www.example.com/de/$1 [R=301,L]

想要排除/folder/路径下的请求不被重定向,添加RewriteCond %{REQUEST_URI} !^/folder/规则后却没有效果,当前完整的.htaccess代码如下:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.es$
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule (.*)$ https://www.example.com/es/$1 [R=301,L]
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.fr$
RewriteRule (.*)$ https://www.example.com/fr/$1 [R=301,L]
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.de$
RewriteRule (.*)$ https://www.example.com/de/$1 [R=301,L]
</IfModule>

# BEGIN WordPress
.....

修复后的配置

把排除规则应用到所有需要排除的重定向规则上,同时优化配置结构减少冗余:

<IfModule mod_rewrite.c>
RewriteEngine On

# 西班牙域名重定向,排除/folder/路径
RewriteCond %{HTTP_HOST} ^www.example.es$
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule (.*)$ https://www.example.com/es/$1 [R=301,L]

# 法国域名重定向,排除/folder/路径
RewriteCond %{HTTP_HOST} ^www.example.fr$
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule (.*)$ https://www.example.com/fr/$1 [R=301,L]

# 德国域名重定向,排除/folder/路径
RewriteCond %{HTTP_HOST} ^www.example.de$
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule (.*)$ https://www.example.com/de/$1 [R=301,L]
</IfModule>

# BEGIN WordPress
.....

关键说明

  1. 补全排除规则:之前仅给西班牙域名的重定向添加了排除条件,其他域名规则未配置,导致访问www.example.fr/folder/或www.example.de/folder/仍会被重定向,需要给所有目标重定向规则都加上排除条件。
  2. 合并冗余配置:重复的<IfModule mod_rewrite.c>和RewriteEngine On可以合并,避免规则执行混乱,同时简化配置结构。
  3. 清除缓存测试:因为使用了R=301永久重定向,浏览器会缓存旧规则,修改后需清除浏览器缓存或用隐私窗口测试,确保新规则生效。

如果需要排除多个文件夹,可修改排除规则为:

RewriteCond %{REQUEST_URI} !^/(folder|another-folder|third-folder)/

这样就能同时排除/folder/、/another-folder/和/third-folder/路径下的请求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 11:21:31