【问题标题】:Laravel and AngularJS SEO WITHOUT hashbang URLLaravel 和 AngularJS SEO 没有 hashbang URL
【发布时间】:2014-10-23 10:08:28
【问题描述】:

我为客户端开发了一个带有 Laravel 后端和 AngularJS 的网站。 我在 AngularJS 中启用了 html5mode,这样 URL 就没有 hashbangs。

使用 grunt 我在快照文件夹中创建了快照。共有5个页面,每个页面作为snapshot__page.html输出到snapshots文件夹。

我在 Larvel 路由文件中使用以下代码将所有请求重定向到索引文件,以便 Angular 可以处理其余部分。

App::missing(function($exception)
{
    return View::make('index');
});

可以通过浏览器访问网站

http://www.domain.com/ 
http://www.domain.com/page1
http://www.domain.com/page2 

等和快照在

http://www.domain.com/snapshots/snapshot__.html
http://www.domain.com/snapshots/snapshot__page1.html
http://www.domain.com/snapshots/snapshot__page2.html

Google 声明要包含

<meta name="fragment" content="!">

这样爬虫就会按如下方式访问页面。

http://www.domain.com?_escaped_fragment=
http://www.domain.com/page1?_escaped_fragment=
http://www.domain.com/page2?_escaped_fragment=

我们可以为这样的请求提供快照。

我的问题在于 .htaccess 文件。

即使当我使用 "?escaped_fragement=" 访问以测试页面时,索引页面也会被加载。这是 Laravel 捕获 404 路由并为索引页面提供服务。 有很多关于如何为 URL 之间出现的 ?escaped_fragment= 提供快照的文档。 这里讨论了一个类似的问题

https://groups.google.com/forum/#!msg/angular/lK8M5bFwbkQ/FN1t1SYD3QkJ .htaccess for SEO bots crawling single page applications without hashbangs

但提供的 .htaccess 似乎不起作用。

我的 .htaccess 文件如下。我在摆弄 .htaccess。

<IfModule mod_rewrite.c>

 ############################################
 ## enable rewrites

Options +FollowSymLinks
RewriteEngine on

#Access site only with www
RewriteCond %{HTTP_HOST}       !domain\.com$
RewriteRule [domain]?(.*) http://www.domain.com/$1 [R=301,L]

#Snapshots for SEO  
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=$
RewriteRule ^(.*)$ /snapshots/snapshot__$1.html [L,NC]


#Laravel Stuff
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]


</IfModule>

【问题讨论】:

    标签: angularjs .htaccess laravel-4 seo single-page-application


    【解决方案1】:

    根据您上面的规范,此行有一个尾随下划线

    RewriteCond %{QUERY_STRING} ^_escaped_fragment_=$

    【讨论】:

      猜你喜欢
      • 2017-07-30
      • 2016-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-09
      • 1970-01-01
      • 2014-12-20
      相关资源
      最近更新 更多