3

A page redirects to another page using a 301 redirect in a .htaccess file. If I try to access the page from a URL with UTM tagging, after redirection, it removes all UTM tagging.

Is this normal behavior? How can we keep the UTM tagging so that we can see this in our analytics?

Here are some examples:

Example 1:

http://example.com/page.html?utm_source=nosource&utm_medium=nomedium&utm_campaign=nocam

redirects to:

http://example.com/another-page.html

Example 2:

http://example.com/page.html?utm_source=nosource&utm_medium=nomedium&utm_campgn=nocam&page=1 (Just misspelled utm_camaign to check if utm_ is not just a culprit)

redirects to:

http://example.com/another-page.html?utm_campgn=nocam&page=1

Example 3:

http://example.com/page.html?utm_source=nosource&utm_medium=nomedium&utm_campaign=nocam&page=1

redirects to:

http://example.com/another-page.html?page=1

Example 4:

http://example.com/page.html?page=1

redirects to:

http://example.com/another-page.html?page=1


Excerpt from .htaccess:

RewriteRule ^home-living/furniture/([^/\.]+)\.html http://www.example.com/home-living/furniture/filter/cat/$1.h‌​tml [R=301,L]
1
  • "If I try to access the page from a URL with UTM tagging, after redirection, it removes all UTM tagging." - That reads as if you are clicking a link on a page, and the page the link is on has UTM tagging? But is the link that is being clicked, and ultimately redirected, have the UTM tagging parameters?
    – MrWhite
    Dec 29, 2016 at 23:58

3 Answers 3

1

If your .htaccess has RewriteRules like these:

RewriteRule page.html /another-page.html [NC,R=301,L]

It will always remove querystring parameters, that's default behaviour. You'll have to add QSA flag to your RewriteRule, like this:

RewriteRule page.html /another-page.html [NC,QSA,R=301,L]

Apache mod_rewrite doc
QSA|qsappend

4
  • tried using QSA but still same behavior
    – Sandesh
    Jul 16, 2014 at 5:38
  • @ganesh could you post your htaccess or at least an excerpt?
    – Binarysurf
    Jul 16, 2014 at 15:02
  • 1
    RewriteRule ^home-living/furniture/([^/\.]+)\.html http://www.example.com/home-living/furniture/filter/cat/$1.html [R=301,L] Query string work fine except utm_source, utm_medium and utm_campaign. Why only these removed from query string?
    – Sandesh
    Jul 17, 2014 at 6:32
  • "It will always remove querystring parameters, that's default behaviour." - Actually, the default is the opposite. If you aren't including a query string on the substitution (as in the example), then the query string is passed through from the request unaltered. However, if you include a query string on the substitution then this will discard the query string on the request. You only need the QSA flag if you explicitly include a query on the substitution and you want to merge this with whatever query string is included on the request.
    – MrWhite
    Dec 29, 2016 at 23:52
1
RewriteRule ^home-living/furniture/([^/\.]+)\.html http://www.example.com/home-living/furniture/filter/cat/$1.h‌​tml [R=301,L]

This directive preserves the entire query string from the original request. It does not remove it, or any part of it. The query string is preserved by default with RewriteRule (and Redirect and RedirectMatch) directives. Only when you explicitly define a new query string on the substition (URL target) is the query string replaced (in its entirety). Only then would you need the QSA (Query String Append) flag in order to append the original query string to the query string stated in the substitution string in order to preserve it - if that is required.

What is described here in examples 2, 3 and 4 is selective removal of specific URL parameters. This is certainly not caused by the above directive. If this is caused by directives in .htaccess at all then there must be a specific section / group of directives elsewhere in the file (or server config) that does this. And this is likely resulting in at least two redirects (which would seem to be implied by your statement: "after redirection, it removes all UTM tagging").

However, if this is the case then I would also expect these utm_ parameters to be removed from any requested URL, regardless of whether that URL is redirected or not.

We would need to see the entire .htaccess file in order to progress with this further, otherwise, this is quite likely to be triggered elsewhere in the application logic, particularly if it's only occurring "after redirection".

0

I suggest you to add this line in you .htaccess files if you want to execute and delete the utm analytics from the URL

#REDIRECTION UTM CLEAR
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} "utm" [NC]
RewriteRule (.*) /$1? [R=301,L,QSD]
</IfModule>
1
  • I edited the code to fix the closing </IfModule> directive (although the <IfModule> wrapper should arguably be removed altogether). However, The OP is not wanting to remove UTM parameters from the URL - the removal of UTM parameters would seem to be "problem" they are trying to resolve. (Aside: But if you are trying to remove UTM parameters then the regex utm is arguably too general and matches too much. Perhaps (?:^|&)utm_\w+= would be preferable. But the RewriteRule removes all URL parameters, not just the utm_ parameters, which may or may not be a problem.)
    – MrWhite
    Aug 29, 2021 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.