首页 文章

如何在.htaccess中mod_rewrite / RewriteRule将所有“_escaped_fragment_”url重定向到静态snpashot html文件夹来处理AngularJS SEO

提问于
浏览
0

我搜索,阅读和实验了一整天,但无法使其工作 .

在我的angularjs网站,我有

$locationProvider.html5Mode(false);
$locationProvider.hashPrefix('!');

所以,我的链接就像,

http://example.com/#!/home
http://example.com/#!/folder1/folder2

根据AngularJS SEO文档,抓取工具会将以上链接视为,

http://example.com/?_escaped_fragment_=/home
http://example.com/?_escaped_fragment_=/folder1/folder2

现在,我的目标是在.htaccess中编写一些规则,将抓取工具重定向到某些快照文件夹和静态html文件 . 因此,当抓取工具尝试转到上述链接时,应将其重定向到,

http://example.com/snapshot/home
http://example.com/snapshot/folder1/folder2

我几乎没有.htaccess文件的经验,也没有Perl Regex的知识 .

请不要只给出链接作为答案,因为我用Google搜索并尝试了很多组合 .

我还提到this stackoverflow link,并尝试使用我的.htaccess文件,但无法将其重定向到快照文件夹 .

谁能帮助我让它工作?

*********************** UPDATE 1 ************************** ******

根据@Olaf Dietsche的回答,我在.htaccess文件中写了这两行(和这两行)

RewriteCond %{QUERY_STRING} _escaped_fragment_=([^&]*)
RewriteRule ^$ /snapshot/%1 [R,L]

结果是,当我打

http://example.com/?_escaped_fragment_=/home

在我的浏览器中,网址正在变为

https://example.com:80/snapshot//home?_escaped_fragment_=/home

预期是 http://example.com/snapshot/home

那么,目前的问题是,

  • 'http'变为'https',因此得到 SSL connection error

  • 一个端口号:80突然出现,但不存在
    快照后

  • 2正斜杠'//'

  • 之后'home'之前'?escaped_fragment=/home'也停留,这不应该存在

请帮忙...

1 回答

  • 2

    我不熟悉Angular,但它不重定向的原因是因为引用问题中的规则只重写而不是重定向 . 另见Understanding difference between redirect and rewrite .htacess .

    要重定向,您必须将另一个域的绝对URL作为目标,或者添加R|redirect标志,例如

    RewriteCond %{QUERY_STRING} _escaped_fragment_=/([^&]*)
    RewriteRule ^$ /snapshot/%1? [R,L]
    

    除非您添加自己的查询字符串,否则查询字符串将不加改变地附加到目标 . 因此,要删除以前的查询字符串,只需添加一个问号即可 .

相关问题