实际上,这是我的 Angular/SEO 经验。
你必须做出很多改变!
1) 从网址中删除#
app.config(['$locationProvider', function ($locationProvider) {
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
}]);
2) 检查您的 MVC 路由
到目前为止,您可能只有一个 HomeController 用于返回 index.cshtml 并启动您的 Angular 应用程序。
从 Angular 路由中删除 # 后,您必须为所有路由设置 MapRoute。
因为在这种情况下,您第一次尝试访问诸如www.site.com/any_route Angular App 之类的路由尚未加载,因此它会尝试从 MVC 路由获取页面。但在那之后$routeProvider 尽职尽责。
3) 将 MVC 变量用于元标记
为了更好地索引并与爬虫和机器人成为朋友,我们必须使用 MVC 变量来初始化网站元标记。
如果您通过 Angular 绑定设置页面标题,例如 <title>{{title}}</title>,每当您想通过社交网络分享您的页面时,您将看到 {{title}},因为社交网络无法呈现网站。
<title>@ViewBag.title</title>
<meta name="Description" content="@ViewBag.description">
<meta name="Keywords" content="@ViewBag.keywords">
<meta property="og:title" content="@ViewBag.title" />
<meta property="og:description" content="@ViewBag.description" />
4) 替换元标记的 Angular 绑定
我们的应用是 SPA,所以在加载 Angular 之后,我们就离开了 MVC 游乐场。
我们必须用 MVC 变量替换 Angular 变量。
angular.element('title').remove();
angular.element('meta[name="Description"]').remove();
angular.element('meta[name="Keywords"]').remove();
angular.element('meta[property="og:title"]').remove();
angular.element('meta[property="og:description"]').remove();
var description = angular.element('<meta name="Description" content="{{meta.description}}">');
angular.element('head').prepend(description);
var keyword = angular.element('<meta name="Keywords" content="{{meta.keywords}}">');
angular.element('head').prepend(keyword);
var titleOg = angular.element('<meta property="og:title" content="{{meta.title}}" />');
angular.element('head').prepend(titleOg);
var descriptionOg = angular.element('<meta property="og:description" content="{{meta.description}}" />');
angular.element('head').prepend(descriptionOg);
var title = angular.element('<title ng-bind="meta.title"></title>');
angular.element('head').prepend(title);
$rootScope.$applyAsync(function () {
$compile(title)($rootScope);
$compile(description)($rootScope);
$compile(keyword)($rootScope);
$compile(titleOg)($rootScope);
$compile(descriptionOg)($rootScope);
});
5) 将JSON-lD 用于动态内容
如果您熟悉SCHEMA.org,则最好使用JSON-LD 而不是其他人,因为搜索引擎机器人可以捕获和分析页面加载后动态插入的<script type="application/ld+json"></script>s。
您必须检查 Schema Dictionary 才能找到最接近您的数据结构的类型。
比如我公司的json-ld:
<script type="application/ld+json">
{
"@context" : "http://schema.org",
"@type" : "Organization",
"name" : "داده کاوان امیرکبیر",
"alternateName" : "ADM | Amirkabir Data Miners",
"description": "شرکت داده کاوان امیرکبیر | تولید کننده نرم افزارهای تحت وب، از قبیل حسابداری آنلاین 'کاج سیستم' ، سیستم مدیریت پروژه 'تسک من' و ...",
"url" : "https://adm-co.net",
"email": "info@adm-co.net",
"logo": {
"@type": "ImageObject",
"url": "http://khoonamon.com/images/ADM_Logo.png",
"caption": "لوگو داده کاوان امیرکبیر",
"width": "2480px",
"height": "1459px"
},
"telephone": "+98-21-44002963",
"address": "تهران، خیابان آیت ا... کاشانی، نبش خیابان عقیل، پلاک 380، طبقه دوم",
"contactPoint" : [{
"@type" : "ContactPoint",
"telephone" : "+98-21-44002963",
"contactType" : "customer service",
"contactOption" : "TollFree",
"areaServed" : "IR",
"availableLanguage" : "Persian"
}],
"sameAs" : [
"https://google.com/+ADMcoNet-GPlus",
"https://www.linkedin.com/company/adm-amirkabir-data-miners-?trk=biz-companies-cym",
"https://instagram.com/AmirkabirDataMiners/",
"https://www.facebook.com/AmirkabirDataMiners",
"http://www.pinterest.com/AmirkabirDM/",
"https://twitter.com/AmirkabirDM",
"https://www.youtube.com/channel/UCQxP0vZA05Pl9GlyXXQt14A/about"
]
}
</script>