vb.net - 如何使打开的图形链接与 301 重定向一起使用
问题描述
我在VB.NET
应用程序上使用友好的 URL,我在社交媒体上共享 URL,图像预览工作正常。链接可以分享如下:
example.com/news/123
或者
example.com/news/123/hello-this-is-an-article
为了避免为同一篇文章共享两个版本的 URL,我创建了一个301 redirect
onPre_load
以将第一个链接重定向到第二个相应的链接。这样,我确保每个访问的 URL 都包含最后的文章标题。
在我应用此逻辑之后,末尾没有文章标题的 URL(第一个 URL)不再在社交媒体上显示图像预览。Twitter 卡验证器和Facebook 开放图形调试器表示存在 301 重定向,并且未找到元标记。
下面是在页面上运行的代码Pre_Load
:
'Comparing URL title with the actual Article title
If strURLTitle <> strArticleTitle Then
'URL article title either changed or does not exist
Response.Status = "301 Moved Permanently"
Response.AddHeader("Location", "example.com/news/{ID-in-the-URL}/article-title")
End If
这是我使用的示例 HTML:
<meta name='author' content='test' />
<meta name='keywords' content='test,test,test' />
<meta name='description' content='test test test' />
<meta itemprop='name' content='test' />
<meta itemprop='description' content='test' />
<meta itemprop='image' content='https://example.com/img.jpg' />
<meta property='og:title' content='test' />
<meta property='og:description' content='test test test' />
<meta property='og:image' content='https://example.com/img.jpg'' />
<meta property='og:image:secure_url' content='https://example.com/img.jpg'' />
<meta property='og:image:width' content='780' />
<meta property='og:image:height' content='439' />
<meta property='og:site_name' content='test' />
<meta property='og:url' content='https://example.com/news/3710/here-is-the-title' />
<meta property='og:type' content='article' />
<meta property='og:locale' content='ar_AR' />
<meta name='twitter:card' content='summary' />
<meta name='twitter:site' content='https://example.com' />
<meta name='twitter:title' content='test' />
<meta name='twitter:description' content='test test test' />
<meta name='twitter:image' content='https://example.com/img.jpg' />
<base href='https://example.com' />
问题:
在 Twitter Card Validator 上测试链接(没有文章标题)会产生以下日志:
信息:页面获取成功
警告:未找到元标记
警告:此卡被重定向到
news/123/hello-this-is-an-article
在 Facebook Debugger 上测试链接(没有文章标题)会产生以下日志:
响应代码 400
获取 URL example.com/news/123
规范 URL
example.com/news/123/hello-this-is-an-article
重定向路径
输入 URL example.com/news/123 301 HTTP
重定向 example.com/news /123/你好,这是一篇文章
有没有办法让打开的图形调试器逃避 301 重定向或显示正确的结果,即使我在最后分享了一个没有标题的链接?
解决方案
由于我在 URL 中使用了阿拉伯字符,因此我需要对 URL 进行编码才能使其在 Facebook 和 Twitter 上运行。
使固定:
Response.Status = "301 Moved Permanently"
Response.AddHeader("Location", "example.com/news/{ID-in-the-URL}/" & HttpUtility.UrlEncode(article-title))
<meta property='og:url' content='https://example.com/news/3710/encoded-url' />
另一个修复:
使用规范的 URL:
<link rel="canonical" href="https://example.com/news/3710/encoded-url" />
推荐阅读
- c# - 打开一个带有要填充的文本区域和一个从页面加载一些内容的文本区域的模式
- sql - Postgres 选择数组具有相等值但没有特定顺序的位置
- primefaces - 执行 p:linkButton 时遇到问题
- php - laravel 在数据库中的刀片页面中附加 html 代码
- css - Firefox 60.0.2 无法正确呈现列表中的列
- hibernate - Spring Boot Rest JPA 更新数据错误
- groovy - Groovy 字符数据类型
- typescript - 如何正确地在 TypeScript 上重写这个类?
- json - 'Newtonsoft.Json.Utilities.JavaScriptUtils' 的类型初始化程序引发异常
- html - 根据滑块(复选框)选择显示/隐藏 div