Crawlable AJAX with _escaped_fragment_ in htaccess

Hello to all the developers!

We are almost done developing the first phase of our ajax web application. In our application, we use hash fragments, such as:

http://ourdomain.com/#!list=last_ads&order=date 

I understand that Google will select this URL and make a request to the server in this form:

 http://ourdomain.com/?_escaped_fragment_=list=last_ads?order=date&direction=desc 

everything is perfect except ...

I would like to redirect this request to another script

So:

 RewriteCond %{QUERY_STRING} ^_escaped_fragment_=(.*)$ RewriteRule ^$ /webroot/crawler.php$1 [L] 

The problem is that when I try print_r ($ _ REQUEST) in crawler.php I get only:

 Array ( [_escaped_fragment_] => list=last_ads?order=date [direction] => desc ) 

I would like to receive

 Array ( [list] => last_ads [order] => date [directions] => des ) 

I know that I can use php to further break the first argument, but I don't want to;)

please inform

==================================================== == EDIT ... some corrections in text and logic

+6
source share
5 answers

Your forgotten QSA directive (everyone missed the dot = D)

 RewriteCond %{QUERY_STRING} ^_escaped_fragment_=(.*)$ RewriteRule ^$ /webroot/crawler.php%1 [QSA,L] 

By the way, your $1 well mistaken ... useless because it means nothing. So this should be:

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

Tell me if this works.

+3
source

If I'm not mistaken.

 RewriteCond %{QUERY_STRING} ^_escaped_fragment_=(.*)$ RewriteRule ^$ /webroot/crawler.php?%1 [L] 
+1
source

This may be obvious to you, but the documentation talks about escaped characters: Configure the server to process requests for URLs containing

The crawler avoids certain characters in the fragment during conversion. To get the original fragment, be sure to cancel all% XX characters in the fragment. More specifically,% 26 should become &,% 20 should become space,% 23 should be #, and% 25 should be%, etc.

+1
source

Here is a solution that allows you to properly configure the URL and request parameters for processing on the server side script.

Example: If you want http://yoursite.com/#!/product/20 become http://yoursite.com/crawler/product/20

First in .htaccess

 RewriteCond %{QUERY_STRING} ^_escaped_fragment_=(.*)$ RewriteRule ^$ /crawler/index.php?_frag=%1 [L] 

We need to get rid of _escaped_fragment_ in the URL and replace it with something else, for example: _frag so that the web server (Apache) does not get into circular rewrites.

Second in crawler/index.php

 <?php if(array_key_exists('_frag', $_REQUEST)) { $_SERVER['REQUEST_URI']=$_REQUEST['_frag']; unset($_REQUEST['_frag']); parse_str($_SERVER['QUERY_STRING'], $frag); parse_str(preg_replace('/^.*\?/', '', $frag['_frag']), $_REQUEST); $_SERVER['QUERY_STRING'] = http_build_query($_REQUEST); } // Continue with your usual script of routing // $_REQUEST now contains the original query parameters 
0
source

In htacess, working in a virtual host does not work, so I add to the "directory"

 <Directory "X:/DIR"> RewriteEngine On RewriteCond %{QUERY_STRING} ^_escaped_fragment_=(.*)$ RewriteRule ^$ /crawler/index.php?_frag=%1 [L] </Directory> 
0
source

Source: https://habr.com/ru/post/901102/


All Articles