N
N
nik0laysviridov2021-04-26 14:15:57
htaccess
nik0laysviridov, 2021-04-26 14:15:57

How to make a redirect with get parameters to without them except utm tags?

It is necessary that in links of the form

https://site.ru/?_escaped_fragment_=annatrish/oss8q
https://site.ru/?cat=3
/?feed=comments-rss2

get-parameters were cut off and the page with the URL before ? was opened. But at the same time, it is necessary that links with UTM tags remain working.

I use this rule:

RewriteCond %{REQUEST_URI} !^utm
RewriteCond %{QUERY_STRING} ^(.+) [NC]
RewriteRule ^(.*)$ /$1? [R=301,L]


GET parameters are removed from the URL, only the exception for utm tags does not work.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nadim Zakirov, 2021-04-26
@zkrvndm

I can tell you how to do all this in JavaScript:

<script>
    window.history.replaceState('', '', location.pathname);
</script>

On the plus side: the tags are removed only in the browser and this does not break the analytics systems in your php code.
Of the minuses: in fact, this is just a disguise and it only works on the browser side.

N
nik0laysviridov, 2021-04-26
@nik0laysviridov

Rule of thumb if anyone needs it
# check that there is indeed a query string
RewriteCond %{QUERY_STRING} ^.+$
# check that it doesn't start with one of allowed parameters
RewriteCond %{QUERY_STRING} !^(utm_source)= [NC]
RewriteRule ^(.*)$ /$1? [R=301,L]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question