Mobile app version of vmapp.org
Login or Join
Yeniel560

: .htaccess 301 Redirect Hash Tag Redirect 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

@Yeniel560

Posted in: #301Redirect #Hash #Htaccess #Redirects

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.

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Yeniel560

2 Comments

Sorted by latest first Latest Oldest Best

 

@Phylliss660

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:
www.example.com/foobar.php#!this-is-a-fragment

would be rewritten by Googlebot into:
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.

10% popularity Vote Up Vote Down


 

@Caterina187

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:


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.
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.
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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme