3

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

  • Have you looked at this: http://stackoverflow.com/questions/1991626/url-rewriting-removing-hash – claesv Feb 28 '12 at 15:39

2 Answers2

1

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.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • Hi Minitech, thanks for that. How do I set up a global javascript to handle incoming requests and redirect etc? Sorry, I'm not very familiar with Apache yet, so I don't know what I can do with it as such at present. Many thanks, Rich – Richard Burnley Feb 29 '12 at 09:26
  • @RichardBurnley: You mean automatically? It's nearly impossible to do that. Just include the script on every page, call it `default.js` or something similar. It gives you the advantage of being able to change the behaviour of any page a little more easily. – Ry- Feb 29 '12 at 15:17
  • Hi Minitech, there are only around 18 key URLs that I need to set up a redirect for, so not too bad if I need to insert a script call. I'm still not sure how I would go about that though, as the incoming request will be for a URL that can't exist on the Apache platform, for example www.yorkshiretea/#/what_we_care_about, which on the new site actually needs to redirect to www.yorkshiretea.co.uk/about-us/yorkshire-respecting-the-planet.php. What do I need to create on my new site that will intercept any requests coming in for that URL, and then redirect as necessary? Cheers, Rich – Richard Burnley Feb 29 '12 at 18:08
  • @RichardBurnley: Hm... if your URLs don't have some kind of fixed translation to them, then you can't automate it. Sorry! – Ry- Feb 29 '12 at 23:53
0

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>
Lee
  • 10,496
  • 4
  • 37
  • 45
  • Hi Lee, thanks for the advice. At the moment I'd settle for just being able to redirect visitors arriving from old links to be honest. How might I go about implementing your suggestion of using javascript to intercept the incoming requests and redirect as appropriate? I'm not very familiar with Apache yet, so I don't know what I can do with it as yet. Many thanks, Rich. – Richard Burnley Feb 29 '12 at 09:22
  • Hi Rich, is there a common structure, for example all old links are "www.website.com/index#pagename" and they need to redirect to "www.website.com/pagename" or is each old link completely different to one another? Could you provide real samples of from=>to links (replace the domain with website.com if you need anonymity) – Lee Feb 29 '12 at 16:58
  • Hi Lee, unfortunately the new site is a complete redesign, so there isn't a clean mapping like that. To give an example, on the old site the page might have been www.yorkshiretea.co.uk/#/what_we_care_about, and on the new site this now needs to redirect to www.yorkshiretea.co.uk/about-us/yorkshire-tea-respecting-the-planet.php As you can see, not much correlation between the old URL and the new. There are only a handful of key URLs from the old site that we want to redirect (around 18 or so), so don't mind a bit of manual intervention for each if there is a workaround. Cheers, Rich – Richard Burnley Feb 29 '12 at 18:04
  • Hi Richard, in that case you can either use pure javascript or redirect all hash tags URL's to a php script and process the redirect behind the scenes. I will provide an example of a pure javascript solution above in just a few minutes – Lee Mar 01 '12 at 10:42