views:

31

answers:

1

I am using ajax to load pages on my website. Each time a page is loaded I change the url in the browser to

http//www.example.com/old/page/#!/new/page/

by setting it through window.loaction using javascript

Now what I want to do is when someone comes to my website by entering the url

http//www.example.com/old/page/#!/new/page/

he should automatically get redirected to

http//www.example.com/new/page/

This is somewhat that happens on facebook too.

Can someone help me out with the required .htaccess code to achieve the same.

Thanks in advance

+2  A: 

I don't think anything past the # symbol in your URL is even visible on the server side. So htaccess, php, etc won't even know the hash is there to begin with. I think in order to pull this off you're going to have to use a client side redirect.

window.onload = function(){
  // First we use a regex to check for the #! pattern in the hash     
  if(window.location.hash.match(/\#\!/i)){
    // If we found a match, use substring to remove the #! and do a redirect
    window.location = window.location.hash.substring(2);
  }
};

This example will redirect the user immediately on page load. Unfortunately doing a redirect in this manner won't help the search engines to reindex your site, but thats just one of the pitfalls of using fancy javascript or hash based URL's.

Greg W
i tried using hte following client scriptrequestedURL = ""+window.location+"";position = requestedURL.search("#!");domainame = "http://localhost"if(position != -1){ newpage = requestedURL.substr(position+2); requestedURL = ""; window.location = domainame+newpage;}but the problem with this script was that it was being executed everytime a ajax call was made
Abhishek Jain
it would be great if you could suggest some way of doing the same through javascript
Abhishek Jain
Added an example. It worked on my web server but you may need to tweak it slightly to fit your environment.
Greg W
i tried the code you have given abovebut still facing the same problem that I had with my codethis code gets implemented every time I load any content even through ajax
Abhishek Jain