Apache rewrite condition for ajax crawling

2019-02-28 22:36发布

问题:

I'm trying implement google ajax crawlable snapshots.

For this I've added following rewrite conditions in .htaccess as follows

RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?(.*)$
RewriteRule ^(.*)$ /snapshots/%1.html? [NC,L]

It works fine but it fails in following situation.

www.mysite.com/#!/

This is crawled as www.mysite.com/?_escaped_fragment_=/ as in the google documents.

I have a prepared index.html in my snapshots directory to serve such requests

But for the above rewrite condition it is searching for .html and that results 403 response. Appreciate if you can help me to fix to serve index.html page for such requests

回答1:

You will need an additional rule for above use case. Insert this rule above your existing rule:

RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/$ [NC]
RewriteRule ^ /snapshots/index.html? [NC,L]

RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?(.+)$ [NC]
RewriteRule ^ /snapshots/%1.html? [NC,L]