S3 上的 Backbone 前端的深层链接

发布于 2025-01-06 05:26:41 字数 971 浏览 4 评论 0原文

我有一个在 S3 上运行的一页 javascript(Backbone) 前端,我希望将几个深层链接重定向到同一个索引文件。通常,您可以在 Apache 中使用 mod_rewrite 来执行此操作,但在 S3 中无法执行此操作。

我尝试将默认错误文档设置为与索引文档相同,并且 这表面上可行,但如果您检查实际的响应状态标头,您会看到页面返回为 404。这显然不好。

还有另一种解决方案,它很难看,但比错误文档 hack 更好:

事实证明,您可以创建 index.html 的副本并将其命名为与子目录相同的名称(减去尾部斜杠),例如,如果我克隆index.html 并将其命名为“about”,并确保 Content-Type 设置为 text/html(在元数据选项卡中)所有对 /about 的请求都将返回新的“about”,它是 index.html 的副本。

显然,这个解决方案不是最优的,并且仅适用于预定义的深度链接目标,但如果克隆 index.html 的步骤是前端构建过程的一部分,则可以减少麻烦。使用 Backbone-Boilerplate,我可以编写一个繁琐的任务来做到这一点。

除了这 2 个 hacky 解决方法之外,除了诉诸 hashbangs 之外,我没有看到其他方法可以做到这一点。

任何建议将不胜感激。


更新:

S3 现在(实际上暂时)支持 索引文档这解决了这个问题。

此外,如果您使用 Route 53 进行 DNS 管理,则可以设置 别名记录 指向您的 S3 存储桶,因此您不再需要子域名+cname :)

I have a one page javascript(Backbone) frontend running on S3 and I'd like to have a couple of deeplinks to be redirected to the same index file. You'd normally do this with mod_rewrite in Apache but there is no way to do this in S3.

I have tried setting the default error document to be the same as the index document, and
this works on the surface, but if you check the actual response status header you'll see the page comes back as a 404. This is obviously not good.

There is another solution, its ugly but better than the error document hack:

It turns out that you can create a copy of index.html and name it simply the same as the subdirectory(minus the trailing slash), so for example if I clone index.html and name it 'about', and make sure the Content-Type is set to text/html (in the metadata tab) all requests to /about will return the new 'about' which is a copy of index.html.

Obviously this solution is sub-optimal and only works with predefined deeplink targets, but the hassle could be lessened if the step to clone index.html was part of a build process for the frontend. Using Backbone-Boilerplate I could write a grunt task to do just that.

Other than these 2 hacky workarounds I dont see a way of doing this other than resorting to hashbangs..

Any suggestions will be greatly appreciated.


UPDATE:

S3 now (for a while actually) supports Index Documents which solves this problem.

Also if you use Route 53 for your DNS management you can set up an alias record pointing to your S3 bucket, so you dont need a subdomain+cname anymore :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黎歌 2025-01-13 05:26:41

不幸的是,据我所知(我经常使用 s3 网站),你是对的。正如你所说,404 hack 是一个非常糟糕的主意,所以你基本上有以下选择:

  • 使用某种常规后端而不是 S3
  • Content-Type 解决方法
  • Hashbangs

抱歉成为持有者坏消息 :)

对我来说,您无法真正将域的根目录定向到 S3 网站,这一事实是我的一些东西的交易破坏者。 mod_rewrite 类型的场景听起来像是另一个很好的例子,但它不起作用。

Unfortunately as far as I know (and I use s3 websites quite a bit) you're right on the money. The 404 hack is a really bad idea as you said, and so you basically have these options:

  • Use a regular backend of some kind and not S3
  • The Content-Type work-around
  • Hashbangs

Sorry to be the bearer of bad news :)

For me, the fact that you can't really direct the root of the domain to S3 websites was the deal breaker for some of my stuff. mod_rewrite-type scenarios sounds like another good example where it just doesn't work.

π浅易 2025-01-13 05:26:41

您是否尝试重定向到哈希?当您提出这个问题时,我不确定此 S3 功能是否可用,但我能够使用文件夹属性的静态 Web 托管部分中的这些重定向规则来解决问题。

<RoutingRules>
    <RoutingRule>
        <Condition>
            <KeyPrefixEquals>topic/</KeyPrefixEquals>
        </Condition>
        <Redirect>
            <ReplaceKeyPrefixWith>#topic/</ReplaceKeyPrefixWith>
        </Redirect>
    </RoutingRule>
</RoutingRules>

其余的在 Backbone.js 应用程序中处理。

Did you try redirecting to hash? I am not sure if this S3 feature was available when you asked this question, but I was able to fix the problem using these redirection rules in static web hosting section of folder's properties.

<RoutingRules>
    <RoutingRule>
        <Condition>
            <KeyPrefixEquals>topic/</KeyPrefixEquals>
        </Condition>
        <Redirect>
            <ReplaceKeyPrefixWith>#topic/</ReplaceKeyPrefixWith>
        </Redirect>
    </RoutingRule>
</RoutingRules>

The rest is handled in Backbone.js application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文