redirecting to my new domain with js

hi!!! so a couple of months ago i switched from neocities to nearlyfreespeech and since i really like it im ready to fully move away from neocities. im trying to set it up so that going from therift.neocities.org/example.html goes to therift.vip/example.html but all the solutions i can find just tell people to use a 301 redirect with htaccess (which i cant do because neocities doesnt support it)

this is what i tried:

<script type="text/javascript">
        var restOfTheUrl = domainName.split('therift.neocities.org')[1] || '';
        function redirect() { window.location.href = "https://therift.vip/" + restOfTheUrl;}
</script>
<body onload="redirect()"></body>

but it returns the “rest of the url” part as “undefined” as you can see on the page here: https://therift.neocities.org/not_found so theres clearly something missing but i cant tell what.

window.location.pathname is what you’re looking for. For example, '/t/redirecting-to-my-new-domain-with-js/3206' is what that will spit out if you run it on this page

function redirect() { window.location.href = "https://therift.vip" + location.pathname;}

that works perfectly thank you!

I personally prefer location.replace(), but your answer works too. This one works very well if you want to type less.

<script>location.replace("https://example.com")</script>

Replace your index.html file with this (or just append this line to it) and it should redirect you.

worth noting you can also do this without javascript by including

<meta http-equiv="refresh" content="0; url=https://therift.vip/" />

in the <head> section :slight_smile:

3 Likes

this is the way, no need to overcomplicate with JS

these two don’t really work for my use case since it doesn"t redirect to the same page on the new domain it just redirects to the index

Looks like you found a JS solution, here’s how you’d make the http redirect work for page → page redirects if anyone checks this out later

On the root page /

  <meta http-equiv="refresh" content="0; url=https://therift.vip/">
  <link rel="canonical" href="https://therift.vip/">

on foo.html

  <meta http-equiv="refresh" content="0; url=https://therift.vip/foo.html">
  <link rel="canonical" href="https://therift.vip/foo.html">

on bar.html

  <meta http-equiv="refresh" content="0; url=https://therift.vip/bar.html">
  <link rel="canonical" href="https://therift.vip/bar.html">

You have to replace the links provided with the links to the page under your wanted domain…

In my snippet same as above post:
(on file aaaa.html)
location.replace("https://therift.vip/aaaa.html")

yeah but i didnt really wanna do that for ~50 pages thats why i wanted it to work automatically

…Oh.
Then, uhhh… maybe the first replies were better :/