APO Preventing utm_ removal from URL

I’ve created an htaccess rule on my server to remove query strings like utm_ etc from my URLs.

RewriteCond %{QUERY_STRING} ^(utm_|amp=|m=) [NC]
RewriteRule ^(.*)$ /$1 [QSD,R=301,L]

The rule works, unless utm_ is in the URL, but my other parameters do work. Since this didn’t make sense, I placed Cloudflare in Development mode, and then utm_ was properly being remove from the URL. However, once I enable Cloudflare again, only utm_ is not removed, amp= and m= is always removed in all cases.

Ironically enough, it looks like there was a recent issue with utm_ and I can’t help but wonder if the two issues are tied together?

Under APO, your origin will never see any query string with utm_ parameters, or any other marketing-related parameters in this list. APO removes those parameters (when no other, dynamic query string element is present) to make these pages cacheable.

No. The issue you linked to was resolved at the time. For a while, a bug was hindering the expected behavior (described above), but that was fixed back then.

1 Like

Thanks for the response sir.

Let me provide an example: https://domain.com/post?utm_source=redirect&utm_medium=htaccess&utm_content=1-3-21

This is an example of a URL that is not having the parameters striped, even though they are removed once I remove Cloudflare.

Any idea why?

The way APO works is by removing these marketing parameters between the edge (Cloudflare network) and the origin. In your browser, the utm_ will still show in the address bar, but since they have been removed from the path requested to the origin, the redirection you’ve set at the origin with the .htaccess directives will not work for these marketing parameters.

In the browser:
Request for https://example.com/post?utm_source=x
Sent to the origin:
https://example.com/post (but you still see the query in the browser’s address bar.)
That URL does not trigger your .htaccess rule, as it does not contain the matching query string. Therefore, there’s no redirect.

Compare to:
Request for https://example.com/post?amp=x
Sent to the origin:
That URL triggers your .htaccess rule, and you get:
301 for https://example.com/ (this URL change will show in the address bar.)

Great explanation.

So Cloudflare is intervening here. I wonder how this affects search rankings. I still have URLs in Google Search Console that show the ?utm_source in the URL. I was hoping to 301 those.

1 Like

It shouldn’t.

Redirecting URLs with utm_ would affect your Analytics software, or someone else’s, like an advertiser’s, or that of a third party linking to your website, and it’s not the best way to set the canonical URL for these links. A redirection would prevent the analytics software JavaScript to be loaded. The way APO handles it is better, as it occurs after JS has run, and only between Cloudflare and edge, not affecting what the browser sees.

Redirection is not the only Google recommended method of setting the canonical address. In a case like this, you’d be better off with a rel=canonical meta tag or header. Please see: Como especificar um canônico com rel=&qout;canonical&qout; e outros métodos | Central da Pesquisa Google  |  Documentação  |  Google for Developers and head to their own community in case of further questions, as they fall outside the scope of the Cloudflare Community.

I should be set then, I have the proper rel=canonical set on my site. I sure would like to see Search Console update those URLs to the proper rel=canonical though.

Appreciate all the help!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.