替换.htaccess中的URL部分并保留查询字符串

时间:2013-08-19 14:19:26

标签: regex .htaccess mod-rewrite

我发送了一封电子邮件给很多,其中包含错误的网址,需要使用.htaccess进行修复。错误的网址:

https://amemoirproject.com/chapters/leaving-tanglewood/?utm_medium=email&utm_campaign=Chapter+15+The+Anointed&utm_content=Chapter+15+The+Anointed+CID_6d1662ca75fabbdcbd66397fc00cdcf4&utm_source=Campaign%20Monitor&utm_term=Chapter%2015%20The%20Anointed%20%20A%20Long-Haired%20Handsome%20Jesus

正确的网址:

https://amemoirproject.com/chapters/the-anointed/?utm_medium=email&utm_campaign=Chapter+15+The+Anointed&utm_content=Chapter+15+The+Anointed+CID_6d1662ca75fabbdcbd66397fc00cdcf4&utm_source=Campaign%20Monitor&utm_term=Chapter%2015%20The%20Anointed%20%20A%20Long-Haired%20Handsome%20Jesus

唯一的区别是目录从leaving-tanglewood更改为the-anointed。我需要保留查询字符串,这应该是302。

建议?

修改

仅当原始错误的URL包含以下查询参数和值时,才会发生此302重定向:

utm_campaign=Chapter+15+The+Anointed

1 个答案:

答案 0 :(得分:1)

通过httpd.conf启用mod_rewrite和.htaccess,然后将此代码放在.htaccess目录下的DOCUMENT_ROOT中:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} (^|&)utm_campaign=Chapter\+15\+The\+Anointed(&|$) [NC]
RewriteRule ^(chapters)/leaving-tanglewood/?$ /$1/the-anointed/ [L,NC,R=302]