Crawlable AJAX with _escaped_fragment_ in htaccess

徘徊边缘 提交于 2019-11-30 14:10:38

Your forgot QSA directive (everyone missed the point =D )

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

By the way your $1 is well err... useless because it refers to nothing. So this should be:

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

Tell me if this works.

If I'm not mistaken.

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

Maybe is obvious for you, but in the documentation talk about escaped characters: Set up your server to handle requests for URLs that contain

The crawler escapes certain characters in the fragment during the transformation. To retrieve the original fragment, make sure to unescape all %XX characters in the fragment. More specifically, %26 should become &, %20 should become a space, %23 should become #, and %25 should become %, and so on.

Here is a solution that provides a routable URL and query parameters correctly set for processing in the server side script.

Example: If you want http://yoursite.com/#!/product/20 to 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 the _escaped_fragment_ in the URL and replace it with something different, example: _frag so that the (Apache) web server does not get in to 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

In htacess work in virtual host not working, so i add in "directory"

<Directory "X:/DIR">
    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^_escaped_fragment_=(.*)$
    RewriteRule ^$ /crawler/index.php?_frag=%1  [L]
</Directory>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!