.htaccess 301 Redirect Hash Tag Redirect

7,861

Solution 1

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.

Solution 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:

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:
7,861

Related videos on Youtube

Keefer
Author by

Keefer

Group Interactive Director in Raleigh, NC. Leader, Coach, Manager, Runner, Cyclist, Father, Husband, Tar Heel, Guitarist

Updated on September 18, 2022

Comments

  • Keefer
    Keefer over 1 year

    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.

  • joosthoek
    joosthoek about 12 years
    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.
  • toomanyairmiles
    toomanyairmiles about 12 years
    @IlmariKaronen indeed, this is why I linked to the dev FAQ and linked out to three other discussions on the same subject.
  • toomanyairmiles
    toomanyairmiles about 12 years
    +1 I was hoping an AJAXer could provide more information for the OP