.htaccess规则无效:保留utm标签移除MailChimp标签求助
解决.htaccess中移除MailChimp标签但保留UTM参数的问题
我太懂这种测试有效但实际跑不起来的挫败感了——问题出在规则执行顺序上!WordPress的默认重写规则带了[L]标志,会直接终止后续规则的执行,你的自定义规则根本没机会被触发。
问题根源
你的移除mc_cid规则放在了WordPress规则块的后面,而WordPress规则的最后一行是:
RewriteRule . /index.php [L]
[L]代表“Last”,意思是匹配到这条规则后,Apache就会停止处理所有后续的重写规则,所以你的自定义规则永远不会被执行。
修复方案
把你的自定义重写规则移到WordPress规则块的前面,同时优化正则表达式让它更精准:
调整后的完整.htaccess内容
# 移除MailChimp的mc_cid及后续参数,保留UTM标签 RewriteEngine On # 处理mc_cid在参数中间的情况(你的示例场景) RewriteCond %{QUERY_STRING} ^(.*)&mc_cid= [NC] RewriteRule ^(.*)$ /$1?%1 [R=301,NE,L] # 处理mc_cid在参数开头的极端情况(防患于未然) RewriteCond %{QUERY_STRING} ^mc_cid=.*$ [NC] RewriteRule ^(.*)$ /$1? [R=301,NE,L] # 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 RewriteEngine On RewriteCond %{HTTP_HOST} ^shelleybassett.com [NC] RewriteRule ^(.*)$ https://www.shelleybassett.com/$1 [L,R=301] RedirectMatch 301 /tag/(.*)/$ /?s=$1 RedirectMatch 301 /disciplines/ /portfolio/ RedirectMatch 301 /collections/ /recipes/ RedirectMatch 301 /family/ /plants/ RedirectMatch 301 /series/ /videos/ #<filesMatch "(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$"> #Header set Cache-Control "max-age=84600, public" #</filesMatch> ## EXPIRES HEADER CACHING ## #<IfModule mod_expires.c> #ExpiresActive On #ExpiresByType image/jpg "access 1 year" #ExpiresByType image/jpeg "access 1 year" #ExpiresByType image/gif "access 1 year" #ExpiresByType image/png "access 1 year" #ExpiresByType text/css "access 1 month" #ExpiresByType application/pdf "access 1 month" #ExpiresByType application/javascript "access 1 month" #ExpiresByType application/x-javascript "access 1 month" #ExpiresByType application/x-shockwave-flash "access 1 month" #ExpiresByType image/x-icon "access 1 year" #ExpiresDefault "access 2 days" #</IfModule> ## EXPIRES HEADER CACHING ## # BEGIN WP-HUMMINGBIRD-CACHING # END WP-HUMMINGBIRD-CACHING
规则细节说明
- 第一条规则:专门匹配
mc_cid在参数中间的场景,捕获它之前的所有内容(也就是你需要保留的UTM参数),重写后只保留这部分内容。[NE]确保UTM参数里的空格、&等特殊字符不会被转义,[L]表示处理完这条规则就停止当前轮次的匹配。 - 第二条规则:处理
mc_cid在参数最开头的极端情况,直接清空查询字符串。 - 位置调整:把这两条规则放在WordPress规则之前,确保它们在WordPress接管请求前先完成参数清理。
另外,你原来的代码里有重复的# END WordPress注释,建议删掉其中一个避免混淆。
内容的提问来源于stack exchange,提问作者Shelley Bassett
相关产品推荐
相关产品推荐

