how can I get location href in all entered urls by js

65 Views Asked by At

I am actually looking for a way to get all the URLs entered so that I can redirect the wrong URLs to a 404 page.

when I hit different url s like localhost:0000/details, because location.href is in index.html, this code is not executed. and I can't figure out the URL to redirect the user to the desired page.

2

There are 2 best solutions below

0
On

The exact answer in your case will depend on how your are serving your HTML pages, but generally, you would configure you're server to handle a 404 not found response by sending back a specific HTML page.

See this section on the MDN '404 Not Found' page regarding custom error pages. This section specifically refers to the Apache server, where you would add the following to a .htaccess file.

ErrorDocument 404 /<your-page-name-here>.html

Hope this helps.

0
On

in this case, we should not look for a solution through HTML and js, and this case can only be controlled through the server.

if I were using GitHub pages, you should create a file called 404.html in the root of the repository so that all 404 errors are redirected to this page.

Creating a custom 404 page for your GitHub Pages site

if you are using the Apache server, read @damonholden 's answer.