Richard Burnley
Richard Burnley

Reputation: 31

Hash character in URLs (accessing and redirecting in Apache)

It looks as though this question has been asked in part by some others, but I can't find the answer I'm looking for specifically, so I thought I'd pose my particular scenario in case anyone is able to help.

We have an old website (developed externally by a third party) that is due to be retired and replaced by a new site designed in house. For reasons best known to themselves, the developers of the old site used the hash character as part of the URL for the old site (www.mysite.com/#/my-content-stuff). To assist with the transition and help with SEO I need to set up 301 redirects for the top performing URLs from the old site. As I'm now discovering however, I'm not able to set up a simple redirect in the .htaccess file as I believe it takes the hash character to be a comment and ignores the remainder of the line. I've tried escape characters, using %23 instead, wildcard matching, nothing seems to work.

As a workaround, I wondered about simply creating dummy files with the same paths and URLs as the old site had, then simply creating HTML redirects within them to drive traffic to the correct new pages, but it looks as though the server is doing something similar regarding the hash character in the URL, and ignoring anything afterit. So, if I create a sub-folder on my news server called '#' and create a file in there called 'test.html', I expected to be able to just go to 'www.myNEWsite.com/#/test.html', but it just takes me to the default root file of my site.

Please can anyone shed any light on how I might get around this? I must admit I'm not that clued up on Apache so I'm having to learn a lot as I go.

Many thanks in advance for any pointers or info anyone can provide.

Cheers,

Rich

Upvotes: 3

Views: 5398

Answers (2)

Lee
Lee

Reputation: 10603

Hash tags cannot be read by the server. They are regarded as locations within the document and are therefore not exposed to the server. The client is the only one whom see's these. The best you could do is use a "meta refresh" tag, or alternatively, you could use javascript to detect the url, and if its one which requires 301 redirection, use "window.location" to move the user to a full url where mod_rewrite or a php page can issue a 301 header.

However neither are SEO friendly and only really solve the issue for users that click onto an old link via an external site

<!-- Put in head tag so the page does not wait to load the content-->
<script type="text/javascript">

if(window.location.hash != "") { 

var h = window.location.hash.match(/#\/?(.*)/i)[1];
switch(h) {
    case "something_old":
        window.location = "/something_new.html";
    break;
    case "something_also_old":
        window.location = "/something_also_new.html";       
    break;  
}

}

</script>

Upvotes: 0

Ry-
Ry-

Reputation: 224906

A hash character in the URL specifies the anchor, and it's not even sent to your webserver. A redirect is impossible on the server side, and the old developer probably did it using JavaScript. Implement fallback URLs without the hash instead, and have a global JavaScript script detect these URLs and redirect automatically.

Upvotes: 1

Related Questions