Redirect page with ID

I want to set a .htaccess redirect from an old link to a section of a new page (i.e mydomain.com/something#id) how do I do it?

  • Dimitris Kalliris
    • Support Team Lead

    Hello there David

    After the chat you had with my colleague Violeta some minutes before, I’d like to follow up here with a little more information.

    An anchor (#something) is not processed by webserver at all, but from browser.

    So any rules in .htaccess that process redirections in which source link has an anchor/hash, they won’t be able to read the anchor part. In order to achieve this, we should use either JS or PHP, but that means that whole URL should be loaded first, including anchor, and not when just “clicking on anchor link”.

    Next snippet is a JS example. It reads current URL, compares the hash/anchor and redirects accordingly:

    (function($) {
    var hash = window.location.hash.substr(1);
    if ( hash == "my-anchor" ) {
    window.location.replace("http://domain.com");
    }
    })( jQuery );

    You can use it via your theme options, if they provide any option for custom JS, or a little plugin like this one: https://wordpress.org/plugins/custom-css-js/

    Warm regards,

    Dimitris