I want a JavaScript code that redirects https://www.example.com/collections/all?sort_by=best-selling
to https://www.example.com/collections/all
in the same domain name using JavaScript.
Asked
Active
Viewed 85 times
-2

James
- 172
- 1
- 1
- 13
-
"I want".. well, then write some code. It's not that difficult. – Oliver Gebert Jun 20 '20 at 07:15
3 Answers
0
Try this, it would redirect to /collections/all
of the current domain:
if (location.href.includes('/collections/all?sort_by=best-selling ')
window.location.replace("/collections/all");

iliya.rudberg
- 739
- 12
- 23
-
Hi @iliya.rudberg, this will redirect any page I visited to `/collections/all`, but I want the visitor to redirect to `/collections/all` only if he's visiting `/collections/all?sort_by=best-selling` – James Jun 19 '20 at 22:26
0
you can also use
window.location.assign
Check out the difference between window.location.assign and window.location.replace answered at https://stackoverflow.com/a/4505846/4987870
-
Hi @Sandeep Shukla, this will redirect any page I visited to `/collections/all`, but I want the visitor to redirect to `/collections/all` only if he's visiting this specific page: `/collections/all?sort_by=best-selling` – James Jun 19 '20 at 22:28
-
The whole idea here is I want to not make spayers spy on my store and see the best seller products – James Jun 19 '20 at 22:29
-
Checking the path is quite easy check this answer ` https://stackoverflow.com/questions/16376438/get-path-and-query-string-from-url-using-javascript` – Jul 06 '20 at 15:05
0
If you use window.location.assign("yoururl")
it causes a new document to load. If you use window.location.replace("yoururl")
it replaces the current doc and replaces the current History with that URL making it so you can't go back to the previous document loaded.
Ref: https://developer.mozilla.org/fr/docs/Web/API/Location/assign Ref : https://developer.mozilla.org/fr/docs/Web/API/Location/replace

Aggestor
- 110
- 1
- 6