Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix redirects with regex #39

Merged
merged 5 commits into from
May 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/root_files/_common/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
2 changes: 1 addition & 1 deletion src/templates/app/includes/url_handler.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand Down