0

I'm trying to redirect link 1 to link 2:

1: http://mysite.com/resources#english

2: http://myothersite.com/collections/a-course-in-miracles-books

As you can see, link 1 is an anchor and it seems that I can't simply add a redirect into the .htaccess file like so:

Redirect 301 /resources#english http://myothersite.com/collections/a-course-in-miracles-books

I found that I could potentially add a rewrite rule, according to http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_ne. The example there is the other way around (link to anchor link), but I tried adding this:

RewriteRule ^/resources#english https://myothersite.com/collections/a-course-in-miracles-books [NE,R]

This didn't work either. The anchor still doesn't redirect at all.

Is there any way of accomplishing what I'm trying to do? Thanks so much!

1

Answer here https://stackoverflow.com/questions/15133023/hash-url-rewrite-in-htaccess

In short, no, the bit after the hash (called a fragment) is not sent to the server and thus cannot be dealt with by the htaccess. The NE (no escape) flag applies to the destination url, not the matched pattern.

As the SE answer linked above suggests you can redirect it client side with a bit o js. Something like this on your resources page.

function locationHashChanged() {
    if (location.hash === "#english") {
        window.location.replace("http://myothersite.com/collections/a-course-in-miracles-books");
    }
}

window.onhashchange = locationHashChanged;
  • Thanks! And I can just place that code directly inside the Joomla article that contains the anchor? Oh, but that makes me think, currently the page containing the anchor is still there, but soon this whole page will be unpublished... so where can I put the code you provided then? – Jutta Duncan Feb 15 '17 at 4:11
  • 1. Yep, as long as your editor isn't stripping javascript, although, I'm guessing you want to only handle incoming external links so you may not need to listen for the onhaschange at all. In any case I would add a call to the locationHashChanged() function at the bottom of the script so it will check on original page load. 2. If you can't leave the original path live then I'm not sure how it could be done. Even if you redirect the whole page first - you cant preserve the hash fragment through a redirect as far as I know. – jamesgarrett Feb 15 '17 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.