var wantedPathname; if (location.pathname.match(/.*\/$/)) { wantedPathname = location.pathname; } else { wantedPathname = location.pathname + "/"; } var wantedLocation; wantedLocation = ("/#" + wantedPathname + location.hash.replace("#/", "")); window.location = wantedLocation;