I'm past all test on my solution so here it is
customUrlSerializer:
parse(url: any): UrlTree {
const dus = new DefaultUrlSerializer();
const splittedUrl = url.replace(/%3D/g, '=').split(/(;[\w\d-]*=[\w\d-]*)/g);
const matrixParams = this.extractMatrixParams(splittedUrl);
const queryParams = this.extractQueryParams(url);
if (matrixParams) {
this.saveOldUrlIncident(url);
}
const newUrl = this.buildNewString(splittedUrl, matrixParams, queryParams);
return dus.parse(newUrl);
}
serialize(tree: UrlTree): any {
const dus = new DefaultUrlSerializer(),
path = dus.serialize(tree);
return path.replace(/%3D/g, '=');
}
private extractMatrixParams(splittedUrl: string[]): string {
return splittedUrl
.filter(el => el.startsWith(';'))
.reduce((a, b) => (a = a + b.substr(1) + '&', a), '')
.slice(0, -1);
}
private extractQueryParams(url: string): string {
return url.lastIndexOf('?') > -1 ? url.substr(url.lastIndexOf('?') + 1, url.length - 1) : '';
}
private buildNewString(splittedUrl: string[], matrixParams: string, queryParams: string): string {
let newUrl = splittedUrl.reduce((a, b) => {
if (b && !b.startsWith(';')) {
if (b.indexOf('?')) {
a = a + b.split('?')[0];
} else {
a = a + b;
}
}
return a;
}, '');
if (matrixParams || queryParams) {
newUrl = newUrl + '?';
}
if (matrixParams) {
newUrl = newUrl + matrixParams;
}
if (queryParams) {
newUrl = newUrl + (matrixParams ? '&' : '') + queryParams;
}
return newUrl;
}
And in app.module, in providers add:
{provide: UrlSerializer, useClass: CustomUrlSerializer},