I'm using _escaped_fragment_ to redirect Google to non-ajax pages. I have the following rewrite rule:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=(.*)$
RewriteRule ^$ /seo.php [QSA,L]

Which works fine except for /, because my rules needs #! to redirect to /seo.php

How do I redirect / to /#! so that Google bot will replace it with _escaped_fragment_ and then be redirected to my /seo.php page ?

  • "How do I redirect / to /#! ?" - A bit confused... your question doesn't appear to relate to the rest of your question text?
    – MrWhite
    Feb 9, 2014 at 20:31
  • I updated the question.
    – Giann
    Feb 10, 2014 at 5:48

1 Answer 1


How do I redirect / to /#!

I don't think you can! At least not in the way you want to, that will be picked up by the Googlebot.

The problem here is that / and /#! are exactly the same URL as far as the server is concerned. The fragment identifier (#!) is never passed to the server. So, when you redirect, you end up with a redirect loop. For example, the following results in a redirect loop:

RewriteRule ^$ /#! [NE,R=301,L]

(The NE flag is required here to prevent the # (special character) being %-encoded.)

You need to be able to set some other condition, that you can check for after the initial redirect, in order to prevent further redirects (a redirect loop). This could be done by either setting a query string (which messes up your pretty URL) or setting a cookie (not supported by Googlebot). So, therefore I don't think you can do this, unless there is some trick I've missed?

HOWEVER, I don't think you need to do this in order to get Google to request the alternative _escape_fragment_ URL for your bare domain. Google provides a mechanism for this in the way of a meta tag:

<meta name="fragment" content="!">

Place this meta tag in the head section of your root document (only). Google should then request www.example.com?_escaped_fragment_=, so your internal rewrite should now work. This is covered in Google's specification.

Just a minor point, in your RewriteCond directive (above) you don't appear to be doing anything with the captured subpattern, so the parentheses can be removed:

RewriteCond %{QUERY_STRING} ^_escaped_fragment_=.*$

In fact, you could probably just check for:

RewriteCond %{QUERY_STRING} ^_escaped_fragment_=
  • I already have the meta fragment. When I fetch the URL with Google Webmaster tools, it doesn't seems to use escaped_fragment and is not redirected.
    – Giann
    Feb 10, 2014 at 22:12
  • Yes, I see the same in my logs too. However, this is not entirely unexpected. Since the meta tag is contained in the page, Google is only going to know that it should use the _escape_fragment_ in future after the initial page request. I would expect Googlebot to make a second request when actually indexing your site with the _escape_fragment_ in this instance. However, I can't confirm this at the current time. Is your site very new? Do you see anything in your access logs?
    – MrWhite
    Feb 11, 2014 at 2:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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