Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hampusn/130b738110b3c31c11e0 to your computer and use it in GitHub Desktop.
Save hampusn/130b738110b3c31c11e0 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