user5726628
user5726628

Reputation:

Removing '#' from URL using htaccess in HTML web page

I am trying to remove the # in the following URL: (www.example.com/#section1). How could I do this using the htaccess file. I am sure this could be done using regular expression, but I am not sure how I would do this.

This is what I have written within the htaccess file RewriteRule ^[#].

Thanks for your help!

Upvotes: 0

Views: 796

Answers (1)

Markus Jarderot
Markus Jarderot

Reputation: 89171

Hashes (#) are not send to the server, so you can't manipulate them on the server.

If you really need to remove them, you would have to use JavaScript on each page.

// Wait for the page to load, and call 'removeHash'.
document.addEventListener("DOMContentLoaded", removeHash);
document.addEventListener("Load", removeHash);

function removeHash() {
    // If there is no hash, don't do anything.
    if (!location.hash) return;

    // http://<domain></pathname>?<search><#hash>

    // Build an URL for the page, sans the domain and hash
    var url = location.pathname;
    if (location.search) {
        // Include the query string, if any
        url += '?' + location.search;
    }

    // Replace the loaded url with the built url, without reloading the page.
    history.replaceState('', document.title, url);
}

Upvotes: 1

Related Questions