Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

How do I do a .htaccess redirect that has a hash tag in it?

Example

Source: domain.com/#/subdir

Target: newdomain.com/whatever/something

Everything I've tried ignores the hash tag and either doesn't redirect at all, or redirects at the top level.

Any help would be appreciated.

share|improve this question
3  
You CANNOT do that .. as hash tag is client-side ONLY and never sent to the server by browser. –  LazyOne Apr 20 '12 at 16:12
2  
How about via client-side JS? –  Keefer Apr 20 '12 at 16:15
1  
Well .. I personally have not worked with this kind of client-side routing .. but you will need something like this: github.com/Ti-webdev/jquery.router or this github.com/wuyuntao/jquery-router. With this you will be able to implement navigations like here grooveshark.com or prostopleer.com –  LazyOne Apr 20 '12 at 18:03
add comment

2 Answers

up vote 1 down vote accepted

Google's developer FAQ has this to say (I'm not really an AJAX guy, so this could be way off, if it is forgive me):-

Question: Can I use redirects to point the crawler at my static content?

Redirects are okay to use, as long as they eventually get you to a page that's equivalent to what the user would see on the #! version of the page. This may be more convenient for some webmasters than serving up the content directly. If you choose this approach, please keep the following in mind:

  1. Compared to serving the content directly, using redirects will result in extra traffic because the crawler has to follow redirects to get the content. This will result in a somewhat higher number of fetches/second in crawl activity.

  2. Note that if you use a permanent (301) redirect, the url shown in our search results will typically be the target of the redirect, whereas if a temporary (302) redirect is used, we'll typically show the #! url in search results.

  3. Depending on how your site is set up, showing #! may produce a better user experience, because the user will be taken straight into the AJAX experience from the Google search results page. Clicking on a static page will take them to the static content, and they may experience avoidable extra page load time if the site later wants to switch them to the AJAX experience.

There is also a pretty good discussion at StackOverflow "Redirect 301 with hash part (anchor) #", another at the mod_rewrite forums, and another at Webmaster World which might give you some pointers. I hope this helps.

share|improve this answer
    
This is correct, but it applies only to Google (and perhaps other search engines), not to normal browsers, and only to fragment identifiers ("hash tags") starting with an exclamation point (!), which Google treats in a special way. Also, the answer you quoted doesn't really make much sense out of context; you need to know how Google rewrites URLs containing such fragments before you can really apply it. –  Ilmari Karonen Apr 24 '12 at 19:37
    
@IlmariKaronen indeed, this is why I linked to the dev FAQ and linked out to three other discussions on the same subject. –  toomanyairmiles Apr 24 '12 at 20:00
add comment

As LazyOne notes in the comments, URL fragment identifiers ("hash tags") are not normally sent to the server in an HTTP request, so they cannot be redirected — or processed in any other way — on the server.

You can rewrite such URLs in JavaScript, if the user's browser supports it and has it enabled. A very basic redirection script could look something like this:

if (window.location == 'http://www.example.com/page.html#foobar') {
    window.location = 'http://www.example.com/other.html#whatever';
}

Documentation for the window.location property can be found e.g. on MDN.


Unfortunately, this trick will generally not work for search engines, which only have very limited (if any) JavaScript support. As toomanyairmiles notes, Google provides a special mechanism for passing so-called "hash-bang" fragment strings to the server.

The way it works is, essentially, that whenever Google's crawler encounters a URL with a fragment string beginning with an exclamation point (!), it rewrites the URL so that the fragment is mapped to a query parameter named _escaped_fragment_ instead. So, for example, the URL:

http://www.example.com/foobar.php#!this-is-a-fragment

would be rewritten by Googlebot into:

http://www.example.com/foobar.php?_escaped_fragment_=this-is-a-fragment

before it sends the request. These query parameters are passed to the server, and so it's possible to process them on the server side, including in rewrite rules. As the FAQ answer quoted by toomanyairmiles says, if you return a normal 200 response or a temporary 302 redirect, Google will normally index the returned page under the original "hash-bang" URL, whereas if you return a 301 permanent redirect, they will index the page under the target URL of the redirect.

Just to be absolutely clear, all this applies only to fragment identifiers starting with an exclamation point. All other fragments are still processed normally (and so not sent to the server) by Google.

share|improve this answer
1  
+1 I was hoping an AJAXer could provide more information for the OP –  toomanyairmiles Apr 24 '12 at 20:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.