diff --git a/src/root_files/_common/404.html b/src/root_files/_common/404.html
index fd5e362a82ba..ee4c38674b96 100644
--- a/src/root_files/_common/404.html
+++ b/src/root_files/_common/404.html
@@ -46,18 +46,20 @@
var pathname = location.pathname.toLowerCase().replace(/\/index(\.html)?/, '');
var base = location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : '');
- var path_name_split = pathname.split('/');
- var prefix_path_items = /^\/br_/.test(location.pathname.toLowerCase()) ? 3 : 2;
- var relative_path = path_name_split.splice(0, prefix_path_items - 1).join('/');
- // var default_lang = all_languages.includes(lang.toLowerCase()) ? lang.toLowerCase() : 'EN';
- var default_lang = 'EN'; // TODO remove this when i18n is ready.
+ var regex = new RegExp('^((\/[A-Za-z0-9-_]+){0,2})(\/('+ all_languages.join('|') +'))((\/[A-Za-z0-9-_]+)+)', 'i');
+ var result = regex.exec(pathname);
+ var prefix = result[1] || '';
+ var default_lang = result[4].toLowerCase() || 'en';
+ var routes = result[5] ? result[5].substr(1).replace('/', '::') : '';
+ var default_lang = 'EN'; // TODO remove this when i18n is ready.
location.replace(
- base + relative_path // /br_branch_name
- + '/' // https://deriv.app/
- + default_lang.toLowerCase() + '?from_404=true&p=' + // en?p=
- path_name_split.slice(-1).join('/').replace(/&/g, '~and~') + // statement
- (
+ base
+ + prefix // /br_branch_name
+ + '/' // https://deriv.app/
+ + default_lang.toLowerCase() + '?from_404=true&p=' // en?p=
+ + routes // statement
+ + (
location.search ? '&q=' + location.search.slice(1).replace(/&/g, '~and~') : ''
) + location.hash
);
diff --git a/src/templates/app/includes/url_handler.jsx b/src/templates/app/includes/url_handler.jsx
index 945279855852..f82d1f5c61ca 100644
--- a/src/templates/app/includes/url_handler.jsx
+++ b/src/templates/app/includes/url_handler.jsx
@@ -22,7 +22,7 @@ const URLHandler = () => (
// Converts paths in query string to HTML5 route and refresh the url
if (query.p !== undefined) {
window.history.replaceState(null, null,
- l.pathname.slice(0) + (query.p || '') +
+ l.pathname.slice(0) + (query.p.split('::').join('/') || '') +
(query.q ? ('?' + query.q) : '') + l.hash
);
}