如何在页面加载时重定向用户,并使Twitter卡仍能正常工作

如何在页面加载时重定向用户,并使Twitter卡仍能正常工作,twitter,http-headers,Twitter,Http Headers,我正在整合到我的网站中,特别是应用程序安装和深度链接功能。为此,我在html页面的标题部分添加了所需的元标记 “我的应用程序”中发布到Twitter的功能共享其他网站的文章。为了共享一个不在我的域中但仍有twitter卡显示的网站url,我制作了一个简单的短url,发布在twitter上,指向我的带有meta标记的html页面,然后将用户重定向到原始网站,但我没有得到想要的结果 首先,我尝试通过在标题中返回301响应代码来重定向用户。这会完全按照我的意愿重定向用户(将我的重定向页面保留在浏览器历

我正在整合到我的网站中,特别是应用程序安装和深度链接功能。为此,我在html页面的标题部分添加了所需的元标记

“我的应用程序”中发布到Twitter的功能共享其他网站的文章。为了共享一个不在我的域中但仍有twitter卡显示的网站url,我制作了一个简单的短url,发布在twitter上,指向我的带有meta标记的html页面,然后将用户重定向到原始网站,但我没有得到想要的结果

首先,我尝试通过在标题中返回301响应代码来重定向用户。这会完全按照我的意愿重定向用户(将我的重定向页面保留在浏览器历史记录之外),但推特不会拾取元标记,因此该卡不会显示

下一步,我尝试在Twitter卡元标记下面使用元标记,如下所示:

<META http-equiv="refresh" content="0;URL=http://www.mywebsite.com">

使用此方法,Twitter卡将正确显示,但现在浏览器中的“后退”按钮已启用。此外,我还了解到,不建议使用这种方法,因为出于安全原因,搜索引擎倾向于从搜索结果中删除这样做的网站


是否有人知道如何重定向用户而不启用浏览器中的“后退”按钮,并且仍然允许评估元标记?我更喜欢不使用Javascript的方法。

我发现了一种解决这个问题的有趣方法

我只需要Twitter元标签,当Twitter是一个在我的网站上查看。因此,当用户或机器人向我的网站发出请求时(Twitter必须这样做才能从meta标签填充卡片信息),我会检查发出请求的用户代理。如果它是一个Twitter机器人(它的用户代理当前是Twitterbot/1.0),那么我返回的页面标题中有一个200响应代码和一个元标记重定向(以防万一)。否则,我返回302响应代码,浏览器立即重定向我在那里的使用

这绕过了后退按钮问题和搜索引擎不喜欢我的站点的元标记重定向问题(因为那里的机器人永远不会看到它们!)

更新

最近有人问我是如何做到这一点的,所以我想我可以举个例子。我在我的服务器上使用C#,但是代码很简单,可以判断您是否使用了不同的语言

/// <summary>
/// Redirects a user to the location related to the given id.
/// </summary>
/// <param name="id"></param>
public ActionResult Index(Int32 id)
{
    // Retrieve details about the short link id passed in
    using (DataEntities context = new DataEntities())
    {
        ShortLink shortLink = context.ShortLinks.Single(s => s.Id == id);

        // If the user agent is a twitter bot (currently Twitterbot/1.0), return the page with a meta redirect (just in case) so Twitter can still read the meta tags.
        if (Request.UserAgent.ToString().ToLower().Contains("twitterbot"))
        {
            TwitterCardModel model = new TwitterCardModel
            {
                Id = id,
                Site = "@YOUR_TWITTER_HANDLE",
                Title = shortLink.Title,
                Description = shortLink.Description,
                RedirectUrl = shortLink.FullUrl,
                ImageUrl = shortLink.ImageUrl
            };

            return View(model);
        }

        // Otherwise, redirect the user to the original page.
        Response.Redirect(shortLink.FullUrl, true);
        return null;
    }
}
//
///将用户重定向到与给定id相关的位置。
/// 
/// 
公共操作结果索引(Int32 id)
{
//检索有关传入的短链接id的详细信息
使用(DataEntities上下文=新DataEntities())
{
ShortLink ShortLink=context.ShortLinks.Single(s=>s.Id==Id);
//如果用户代理是twitter机器人(当前为Twitterbot/1.0),则返回带有元重定向的页面(以防万一),这样twitter仍然可以读取元标记。
if(Request.UserAgent.ToString().ToLower()包含(“twitterbot”))
{
TwitterCardModel=新TwitterCardModel
{
Id=Id,
Site=“@YOUR_TWITTER_HANDLE”,
Title=shortLink.Title,
Description=shortLink.Description,
重定向URL=shortLink.FullUrl,
ImageUrl=shortLink.ImageUrl
};
返回视图(模型);
}
//否则,将用户重定向到原始页面。
重定向(shortLink.FullUrl,true);
返回null;
}
}
如果请求来自twitter bot,则这是我返回的HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    @* Twitter cards *@
    <meta name="twitter:card" content="summary" />
    <meta name="twitter:site" content="@Model.Site">
    <meta name="twitter:title" content="@Model.Title" />
    <meta name="twitter:description" content="@Model.Description" />

    @if (!String.IsNullOrEmpty(Model.ImageUrl))
    {
        <meta name="twitter:image" content="@Model.ImageUrl">
    }

    <meta name="twitter:app:name:iphone" content="YOUR APP NAME"/>
    <meta name="twitter:app:id:iphone" content="YOUR APPLE APP ID"/>
    <meta name="twitter:app:url:iphone" content="YOUR DEEP LINK TO YOUR CONTENT IN YOUR APP"/>

    @* Handle page redirect to the full article *@
    <meta http-equiv="refresh" content="0;URL=@Model.RedirectUrl">
    <title></title>
</head>
<body>
</body>

@*推特卡片*@
@如果(!String.IsNullOrEmpty(Model.ImageUrl))
{
}
@*句柄页面重定向到整篇文章*@

我也在做同样的事情,但对我来说,手机似乎不起作用。你在手机上试用过吗?iOS版推特手机应用程序和手机网站都对我有用。什么部分对你不起作用?如果我点击twitter移动网站上的卡片链接,它的用户代理字符串中也有“Twitterbot/1.0”,所以我得到的是元页面而不是重定向。看起来twitter将卡片上的链接转换/代理为t.co/。。。并使用Twitterbot用户代理发出请求。这是我的错误。我启用了清漆缓存。我只是提出了不可撤销的请求。它按预期工作。