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

How to Redirect Anchor Urls in .htaccess

which way 15320861 150x150 - How to Redirect Anchor Urls in .htaccessTask

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.

example:
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.

Solution

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:

ad banner 619 3 - How to Redirect Anchor Urls in .htaccess
<head>
<script>
window.onload = function() {
if(location.hash === “#anchor1”)
location.assign(“page2.htm#anchor2”);
else if(location.hash === “#anchor3”)
location.assign(“page2.htm”);
else if(location.hash === “#Applications”)
location.assign(“JTAG-Applications.htm”);
// etc… //
}
</script>
</head>

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:

ad banner 619 2 - How to Redirect Anchor Urls in .htaccess

Check Also

HTML551 310x165 - 5 Notable HTML5 Trends to Follow in 2018

5 Notable HTML5 Trends to Follow in 2018

HTML5 is the current version of the HTML standard, which is a markup language that …

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