Abhishek Jain
Abhishek Jain

Reputation: 33

htaccess redirection for #! in urls

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

Upvotes: 2

Views: 623

Answers (1)

Greg W
Greg W

Reputation: 5239

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.

Upvotes: 2

Related Questions