【问题标题】:Angular: Display sitemap generated from APIAngular:显示从 API 生成的站点地图
【发布时间】:2020-04-13 18:02:32
【问题描述】:

我有一个 Angular 应用程序和一些 dotnet Web API。出于 SEO 目的,我需要通过 Url 从我的 Angular 应用程序中提供站点地图,例如https://myapp.com/sitemap

站点地图本身由其中一个 API(例如 https://api1.com/sitemap)生成和提供服务,并返回完整的站点地图。

在 google 网站管理员工具中,您只能添加来自当前域的站点地图链接,在我的例子中是 https://myapp.com

目前,我正在纠结如何将https://myapp.com/sitemap 之类的路由添加到我的 routes.ts 中,以便从 API 提供站点地图,而没有 Angular 应用程序的其他内容。 p>

编辑:我的 Angular 应用的网络服务器的 .htcaccess

# This is for other websites in webserer dir which uses php
AddType application/x-httpd-php71 .php

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{HTTP_HOST} !=admin.kombas.de
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]

# Sitemap: Need rewrite for https://kombas.de/sitemap
RewriteRule "^/sitemap" "https://kombasapicontent.azurewebsites.net/api/data/sitemap" [L,P]
#RewriteRule ^/sitemap https://kombasapicontent.azurewebsites.net/api/data/sitemap [L,P] # No quotes make no difference
#RewriteRule "sitemap" "https://kombasapicontent.azurewebsites.net/api/data/sitemap" [L,P] # Only visible reaction => The requested URL was not found on this server.

# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html

不知何故,我没有任何重写工作。

【问题讨论】:

    标签: angular asp.net-core-webapi sitemap


    【解决方案1】:

    使用 javascript 生成站点地图可能是可能的,但不是最好的方法(爬虫运行你的 Angular 应用程序的缓慢和不必要的开销)。 如果你想在 api 上生成站点地图,你可以直接交付它,而不需要使用任何 javascript 框架。您可以使用 Web 服务器的重写规则配置 /sitemap 路由到任何 api 控制器,例如重写/sitemap to /api/sitemap

    【讨论】:

    • 据我的研究表明,这是不可能的,就像你写的那样:{ path: 'sitemap', redirectTo: '/api/sitemap', pathMatch: 'full' }, 仅适用于应用程序的内部 URL/路由 - 不适用于外部,我的 API 是外部 URL .
    • 对不起,你的意思是直接在网络服务器上。我会试试的,谢谢。
    • 我编辑了我的初始帖子,因为我没有为我的 Angular 应用程序在我的网络服务器 (Apache) 上使用这个 htaccess 进行任何重写。
    • 您的 api 是否在您的前端以外的其他域上运行?然后你需要反向代理请求。在最后一行之前尝试RewriteRule "^/sitemap" "https://api1.com/sitemap" [P]
    • 不,它不起作用。我仍然在我的 Angular 应用程序中登陆我的 routes.ts 中定义的错误组件,例如{ path: '**', component: ErrorComponent}。是的,API 正在 Azure 中的另一个域上运行。
    猜你喜欢
    • 2017-11-29
    • 2015-08-11
    • 2010-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多