Redirection from old site page to new site - specific place on page

Hi All,

Hoping someone can help. We have retired an old domain and setup redirects from the sold site to the new sites corosponding pages, on one of the pages we want the old link to point to a new page at a specific point on the page. I have set a name in the ID section and then linked from the old page to new like https://www.*******.co.uk/test.html#specificplaceonpage

If I use this just in a browser typed directly in it works fine but I have a redirect setup in cpanel so we don’t loose out on SEO built up over the years, when the old link is clicked the new page address shows as a 404, in the address bar the # has been replaced by %23 which is why there is a 404.

Can redirect on cpanel not handle the # symbol or is there another way around it?

Regards John

Unfortunately redirects don’t support HTML anchors, they are designed for full page URLs only.

Your hash key gets URL encoded, hence the %23 in the URL.

You can add this JS to your new site that listens for if someone was redirected from the old site to the new site and only runs then to add the HTML anchor back in. You’ll need to update the URL and anchor in this code.

document.addEventListener("DOMContentLoaded", function () {
    const referrer = document.referrer;
    const currentUrl = window.location.href.split("#")[0]; // Remove existing anchors

    if (referrer.startsWith("https://example.com/test-page")) {
        // Append the anchor if it’s not already present
        if (!window.location.hash) {
            window.location.replace(currentUrl + "#anchor");
        }
    }
});
2 Likes

If I had bought the old domain, the first thing I would have done is point it to my server, ensuring the previous owner no longer had control.

If the domain is under your control, it’s best to set up redirects at the server level (Apache .htaccess, Nginx, or a Node.js middleware) instead of relying on cPanel.

Example for Apache:

Redirect 301 /old-page https://www.newsite.co.uk/test.html#specificplaceonpage

This ensures users are properly redirected with the fragment intact.

1 Like

The reason cPanel might encode the URL. In URLs, spaces are not allowed, so they get encoded as %20. For example, ‘this is the new test.html’ would be encoded as ‘this%20is%20the%20new%20test.html’.

This is common behavior when working with URLs, and cPanel may automatically encode the spaces (and other special characters) when setting up a redirect, ensuring the URL is valid and properly formatted. You should manually set up the redirect in your .htaccess file (if you’re using Apache) or directly in the nginx.conf file (for Nginx servers), where you can control how the URL is structured.

Here is a working example for an Nginx server. In the nginx.conf, I redirect the old page to a specific place on another site like this:

location /old-page {
  return 301 https://kuligaposten.com/exchange#table-1;
}

If you click on this link https://example.kuligpost.com/old-page , you will be reirected to https://kuligaposten.com/exchange#table-1. The hashtag will appear as #, not %23, and the redirected page will first load on the client-side not on the server-side, with the browser automatically scrolling to the element associated with the hashtag.
The reason the redirection works as expected (with the # symbol instead of %23) is because the redirection is handled on the client-side. When a browser encounters a URL with a hashtag (e.g., https://kuligaposten.com/exchange#table-1), the browser automatically processes the fragment identifier (the part after the #) locally, on the client-side, without making another request to the server.

@catkin
if the location.href is https://www.newsite.co.uk/test.html%23specificplaceonpage your script will change the url to https://www.newsite.co.uk/test.html%23specificplaceonpage#anchor

The intention would be to not use the anchor in the redirect and then apply it with the JS, which is explained in my post.

Instead of just doing a server-side redirect to the right page and spot on the page, you’re suggesting having two redirects, which might mess with SEO a bit.

It’s just appending the anchor to the URL, not redirecting the user a second time. I haven’t tested it appending the anchor does cause the page to scroll to a section. It’s just one quick suggestion based on the information and details supplied by @johntankard

You think that if you replace the page’s location, the page doesn’t get reloaded or redirected to the new location, even if it’s just a hashtag appended to the URL. However, the page will be reloaded, and it is a redirection.

window.location.replace(currentUrl + "#anchor") replaces the current URL with the same URL but with #anchor, causing a full-page reload and redirection.