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!


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.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? 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. Feb 15 '17 at 21:23
  • I found this thread when I was looking for a solution to this exact problem. The script offered in James's answer works. You can see it in action here: this url with anchor https://www.nationaleberoepengids.nl/onderwijsassistent#salaris redirects to a new page on this topic. A big bonus is that Google seems to follow these clientside redirects, you can find it in this Google video Aug 11 '21 at 6:20

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.