Skip to content

Instantly share code, notes, and snippets.

  • Save lusareal/0afe08efafdc1b7ca82ca0c22facbb17 to your computer and use it in GitHub Desktop.
Save lusareal/0afe08efafdc1b7ca82ca0c22facbb17 to your computer and use it in GitHub Desktop.
# Google Analytics & Campaign Monitor
# Removes all query params where the key begins with "utm_"
# Skip the rewrites if no "utm_*" key exist.
RewriteCond %{QUERY_STRING} !(^|&)utm_ [NC]
RewriteRule .* - [S=3]
# Check and handle first query param.
RewriteCond %{QUERY_STRING} ^utm_[A-Za-z0-9]+=[^&]+(.*) [NC]
RewriteRule (.*) /$1?%1 [R=301,L]
# Check and handle every other query param.
RewriteCond %{QUERY_STRING} (.*)([?|&]utm_[A-Za-z0-9]+=[^&]+)(.*) [NC]
RewriteRule (.*) /$1?%1%3 [R=301,L]
# Minor cleanup. Removes an ampersand if it is the first character in the query string.
RewriteCond %{QUERY_STRING} ^&(.*) [NC]
RewriteRule (.*) /$1?%1 [R=301,L]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment