添加.htaccess重定向后WordPress出现过多重定向循环错误求助
解决WordPress .htaccess重定向循环问题
咱们来拆解你遇到的核心问题:mod_alias的redirect指令是前缀匹配,加上WP内置的旧slug重定向机制,两者叠加导致了无限循环。结合你的配置和场景,我给你一步步的解决方案:
1. 修复.htaccess中的重定向规则(关键!)
你当前使用的Apache redirect指令属于mod_alias模块,它是前缀匹配而非精确匹配。比如你写的redirect 301 /blog /cycling-blog,会把所有以/blog开头的URL(包括你的文章URL/blog/cycling-beginners-guide...)都重定向到/cycling-blog/[后缀],而这些后缀页面不存在时,WP的内置重定向会试图把它转回到原来的/blog/[后缀],形成循环。
替换方案:改用精确匹配的RedirectMatch规则
把所有redirect指令替换为精确匹配的规则,放在# BEGIN WordPress之前,确保只重定向你指定的单个页面/目录,不影响子路径:
# 精确匹配单个页面/目录的301重定向 RedirectMatch 301 ^/routes/gloucestershire-route/cheltenham-hillclimb-blitz-two-cycling-route$ /gloucestershire-cycling-routes RedirectMatch 301 ^/routes/gloucestershire-route/gloucester-follow-severn-cycling-route$ /gloucestershire-cycling-routes RedirectMatch 301 ^/routes/gloucestershire-route/gloucester-steady-cycling-route$ /gloucestershire-cycling-routes RedirectMatch 301 ^/routes/gloucestershire-route/gloucestershire-campus-tour-cycling-route$ /gloucestershire-cycling-routes RedirectMatch 301 ^/routes/gloucestershire-route/lechlade-cotswolds-cycling-route$ /gloucestershire-cycling-routes RedirectMatch 301 ^/routes/gloucestershire-route/stroud-airfield-loop-cycling-route$ /gloucestershire-cycling-routes RedirectMatch 301 ^/routes/gloucestershire-route/stroud-cycle-circuit-cycling-route$ /gloucestershire-cycling-routes RedirectMatch 301 ^/routes/gloucestershire-route/stroud-newport-cycling-route$ /gloucestershire-cycling-routes RedirectMatch 301 ^/routes/gloucestershire-route/forest-dean-newent-woods-cycling-route$ /gloucestershire-cycling-routes RedirectMatch 301 ^/routes/gloucestershire-route/forest-dean-st-bravels-cycling-route$ /gloucestershire-cycling-routes RedirectMatch 301 ^/routes/gloucestershire-route/cirencester-oxford-cycling-route$ /gloucestershire-cycling-routes RedirectMatch 301 ^/routes/gloucestershire-route/cirencester-evesham-cycling-route$ /gloucestershire-cycling-routes RedirectMatch 301 ^/routes/gloucestershire-route/cirencester-countryside-loop-cycling-route$ /gloucestershire-cycling-routes RedirectMatch 301 ^/routes/gloucestershire-route/chipping-campden-bourton-water-cycling-route$ /gloucestershire-cycling-routes RedirectMatch 301 ^/routes/gloucestershire-route/cheltenham-malvern-cycling-route$ /gloucestershire-cycling-routes RedirectMatch 301 ^/routes/gloucestershire-route/cheltenham-bourton-water-cycling-route$ /gloucestershire-cycling-routes RedirectMatch 301 ^/features/what-are-trail-dogs-and-adventure-dogs$ /cycling-blog RedirectMatch 301 ^/shropshire$ /shropshire-hub RedirectMatch 301 ^/south-wales$ /south-wales-hub RedirectMatch 301 ^/gloucestershire$ /gloucestershire-hub # 只精确匹配根目录下的/blog,不影响/blog/开头的文章URL RedirectMatch 301 ^/blog$ /cycling-blog # BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress # BEGIN WP-HUMMINGBIRD-GZIP # END WP-HUMMINGBIRD-GZIP # BEGIN WP-HUMMINGBIRD-CACHING # END WP-HUMMINGBIRD-CACHING
2. 禁用WordPress内置的旧Slug重定向
WP默认会记住文章/分类的旧slug,自动触发301重定向,这也是循环的诱因之一。你可以通过以下方式禁用它:
- 打开主题的
functions.php文件,添加以下代码:
remove_action('template_redirect', 'wp_old_slug_redirect');
- 或者使用代码片段插件(比如Code Snippets)添加这段代码,避免主题更新后代码丢失。
3. 清除缓存(必做!)
301重定向会被浏览器、服务器强烈缓存,所以你需要:
- 清除浏览器缓存和Cookie,或者用无痕模式测试
- 在WP后台清除Hummingbird插件的缓存
- 联系主机提供商清除服务器端缓存(如果有配置的话)
4. 验证HTTPS配置
确保WP的HTTPS配置正确,打开wp-config.php文件,确认以下两行存在:
define('WP_SITEURL', 'https://cyced.co.uk'); define('WP_HOME', 'https://cyced.co.uk');
避免HTTP与HTTPS之间的隐形重定向循环。
内容的提问来源于stack exchange,提问作者Ben King




