Note that the _escaped_fragment_
URL parameter was part of Google's AJAX crawling specification (deprecated since Oct 2015) using #!
style URLs - so make sure your site is not making use of this still.
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=(.*)$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%1? [L,R=301]
This is the right idea, in that you need to examine the value of the QUERY_STRING
server variable, however, the target URL is possibly incorrect. (It might have been correct if you were still using this specification.)
Try something like the following instead:
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=
RewriteRule (.*) /$1 [QSD,R=302,L]
The above should redirect a URL of the form example.com/somedir/3.html?_escaped_fragment_=<anything>
to example.com/somedir/3.html
.
The QSD
flag (Apache 2.4+) is necessary to remove the query string from the target URL.
Note that this is a 302 (temporary) redirect. Only change to a 301 (permanent) redirect once you have confirmed it is working OK - in order to avoid caching issues. 301 redirects are cached persistently by the browser (including any erroneous redirects).
User enters this link: example.com/somedir/3.html?_escaped_fragment_=
Users wouldn't normally enter links like this. They shouldn't even be linked to or picked up by search engines unless there was perhaps a site-misconfiguration at some point? So, depending on how/where these URLs are being linked from (check the report in GSC) then you could even consider blocking these URLs instead? For example:
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=
RewriteRule ^ - [F]
The above would return a 403 Forbidden instead.