Skip to content

Instantly share code, notes, and snippets.

@jakubkulhan
Last active January 23, 2017 13:41
Show Gist options
  • Save jakubkulhan/ac02cf43568d3156871f to your computer and use it in GitHub Desktop.
Save jakubkulhan/ac02cf43568d3156871f to your computer and use it in GitHub Desktop.
Redirect utm_* from QS to hash in Nginx
server {
# ...
location / {
set $utm_hash "";
if ($args ~* "^(.*&?)utm_source=([a-zA-Z0-9._-]*)(&(.*))?") {
set $args $1$4;
set $utm_hash "$utm_hash&utm_source=$2";
}
if ($args ~* "^(.*&?)utm_medium=([a-zA-Z0-9._-]*)(&(.*))?") {
set $args $1$4;
set $utm_hash "$utm_hash&utm_medium=$2";
}
if ($args ~* "^(.*&?)utm_campaign=([a-zA-Z0-9._-]*)(&(.*))?") {
set $args $1$4;
set $utm_hash "$utm_hash&utm_campaign=$2";
}
if ($args ~* "^(.*&?)utm_content=([a-zA-Z0-9._-]*)(&(.*))?") {
set $args $1$4;
set $utm_hash "$utm_hash&utm_content=$2";
}
if ($args ~* "^(.*&?)utm_term=([a-zA-Z0-9._-]*)(&(.*))?") {
set $args $1$4;
set $utm_hash "$utm_hash&utm_term=$2";
}
if ($args ~* "^(.*)&$") {
set $args $1;
}
if ($utm_hash ~* "^&(.*)$") {
set $utm_hash $1;
}
set $redirect "";
if ($utm_hash != "") {
set $redirect "H";
}
if ($args != "") {
set $redirect "Q$redirect";
}
if ($redirect = "QH") {
rewrite "(.*)" $1?$args#$utm_hash? permanent;
}
if ($redirect = "H") {
rewrite "(.*)" $1#$utm_hash? permanent;
}
# ...
}
# ...
}
@tomasfejfar
Copy link

❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment