Home » IT & Tech Blogs » Information Technology » Programming » How to Redirect Anchor Urls in .htaccess

How to Redirect Anchor Urls in .htaccess


So, you have anchor urls that were crawled by Google (ex: http://domain.com/page.html#anchor) and now you need to redirect them.  You want to redirect urls with anchors in them to other urls with and without anchors in .htaccess, but there is an issue doing this in .htaccess because you want to redirect to different pages depending on what the original anchor was.

redirect 301 /subdir/page1.htm#anchor1      http://www.domain.com/subdir/page2.htm#anchor2
redirect 301 /subdir/page1.htm#anchor3      http://www.domain.com/subdir/page2.htm

First, you cannot use the # character (anchor) in .htaccess because it will be seen as a comment.  You might be able to try to do a mod-rewrite but there are complications in doing this for this particular function in .htaccess.


To accomplish this, the only option may be to use JavaScript.  Add the following Javascript to the old url which looks at the requested anchor (location.hash) and triggers a redirect to the appropriate new page or directly right to your new content (with or without an anchor appended).

javascript code:

window.onload = function() {
if(location.hash === “#anchor1”)
else if(location.hash === “#anchor3”)
else if(location.hash === “#Applications”)
// etc… //

While this does not help with SEO nor does not tell Google that the page has moved, it redirects your visitor in the meantime allowing you to not lose any traffic while your new pages get situated.  Hope this helps!

Basic redirect with Javascript:

Check Also

How To Use Printed Catalogs To Drive Online Sales in 2019

Until the recent few years, catalogs were mainly preferred as the primary source of advertising …

Do NOT follow this link or you will be banned from the site!