Is it possible to 301 redirect anchors to a new site using .htaccess? #140

Closed
opened 2016-12-30 06:13:19 +00:00 by privacytoolsIO · 6 comments
privacytoolsIO commented 2016-12-30 06:13:19 +00:00 (Migrated from github.com)

Example:
https://www.privacytools.io/#vpn

Redirect to a new folder:
https://www.privacytools.io/vpn/

Just in case we split the website into more sub pages.

Example: https://www.privacytools.io/#vpn Redirect to a new folder: https://www.privacytools.io/vpn/ Just in case we split the website into more sub pages.
joas8211 commented 2016-12-30 07:21:20 +00:00 (Migrated from github.com)

You cannot use .htaccess or PHP or anything in the host side to manipulate the fragment. Fragment is not sent to the server by default.

You can use javascript to redirect based on fragment.

if (document.location.hash.length > 0) {
  document.location.href = document.location.hash.substr(1);
}

But you cannot do 301 redirect using anchors unless you send the anchor to the server.

if (document.location.hash.substr(1).length > 0) {
    var form = document.createElement("form");
    form.method = "POST";
    var input = document.createElement("input");
    input.name = "fragment";
    input.value = document.location.hash.substr(1);
    document.location.hash = "";
    form.appendChild(input);
    form.submit();
}
if (isset($_POST["fragment"])) {
    header("HTTP/1.1 301 Moved Permanently"); 
    header("Location: " . $_POST["fragment"]);
}
You cannot use .htaccess or PHP or anything in the host side to manipulate the fragment. Fragment is not sent to the server by default. You can use javascript to redirect based on fragment. ```javascript if (document.location.hash.length > 0) { document.location.href = document.location.hash.substr(1); } ``` But you cannot do 301 redirect using anchors unless you send the anchor to the server. ```javascript if (document.location.hash.substr(1).length > 0) { var form = document.createElement("form"); form.method = "POST"; var input = document.createElement("input"); input.name = "fragment"; input.value = document.location.hash.substr(1); document.location.hash = ""; form.appendChild(input); form.submit(); } ``` ```php if (isset($_POST["fragment"])) { header("HTTP/1.1 301 Moved Permanently"); header("Location: " . $_POST["fragment"]); } ```
Vincevrp commented 2019-01-18 14:49:20 +00:00 (Migrated from github.com)

Closing this since we're hosted on Github pages, which doesn't support editing .htaccess.

Closing this since we're hosted on Github pages, which doesn't support editing `.htaccess`.
privacytoolsIO commented 2019-04-01 03:03:34 +00:00 (Migrated from github.com)

I'm late, but thanks @joas8211

We'll look into it now.

I'm late, but thanks @joas8211 We'll look into it now.

We don't want to use Javascript. We have full nginx control so let's look into redirects there.

We don't want to use Javascript. We have full nginx control so let's look into redirects there.
privacytoolsIO commented 2019-04-01 03:04:49 +00:00 (Migrated from github.com)

Closing this since we're hosted on Github pages, which doesn't support editing .htaccess.

We just moved away from GitHub pages. Let's see what we can do

> Closing this since we're hosted on Github pages, which doesn't support editing `.htaccess`. We just moved away from GitHub pages. Let's see what we can do

Actually, we may need to use Javascript on second thought if we wish to preserve the /#vpn, etc links after switching to multiple pages, since the browser doesn't send hash fragments to the server at all 🙁

Which I now see was brought up in the second comment.

Actually, we may need to use Javascript on second thought if we wish to preserve the `/#vpn`, etc links after switching to multiple pages, since the browser doesn't send hash fragments to the server at all 🙁 Which I now see was brought up in the second comment.
This repo is archived. You cannot comment on issues.
No Milestone
No Assignees
1 Participants
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: privacyguides/privacytools.io#140
No description provided.