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


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.


2 Answers 2


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:


would be rewritten by Googlebot into:


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.

  • 1
    +1 I was hoping an AJAXer could provide more information for the OP Apr 24, 2012 at 20:12

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.

  • 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. Apr 24, 2012 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. Apr 24, 2012 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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