如何重定向内部 URL 以在 s3 网站中以斜杠结尾 [英] How to redirect internal URLs to end with trailing slashes with s3 websites

查看:75
本文介绍了如何重定向内部 URL 以在 s3 网站中以斜杠结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 s3 中托管了一个网站,使用 cloudfront 进行缓存.我想将我的内部页面重定向到末尾带有斜杠的页面,当前场景是两个版本(有和没有斜杠)都在工作,应该只使用尾部斜杠,例如:https://us.springverify.com/api-integrations/ 包含所有内部页面.该域在 Route53 中.由于它在 S3 中,我将无法为此使用 .htaccess 文件重定向.有什么办法可以从 cloudfront 或 S3 进行这种重定向(AWS 表示他们不能从 Route53 端进行重定向).任何帮助将不胜感激

I have a website hosted in s3 with cloudfront for caching. I want to redirect my internal pages to pages with trailing slashes at the end and current scenario is both versions (with and without trailing slash) are working, should be only working with trailing slash eg : https://us.springverify.com/api-integrations/ with all internal pages. The domain is in Route53. As it is in S3 I won't be able to use .htaccess file redirection for this. Is there any way i can make this redirection from cloudfront or S3 (AWS said they can't do it from Route53 end). Any help will be greatly appreciated

更新:所以我的确切要求是谷歌将处理 SEO 相关的事情 https://us.springverify.com/api-integrations(不带斜线)和 https://us.springverify.com/api-integrations/ 作为不同的网站.它也发生在其他内部页面上.所以我想要的是即使 URL 被输入或定向到 https://us.springverify.com/api-integrations(不带斜杠)URL 应该重定向到 https://us.springverify.com/api-integrations/(带有斜杠).我如何使用 Cloudfront 实现这一点.如果您需要任何其他信息,请告诉我.

UPDATE: So my exact requirement is for SEO related things google will treat https://us.springverify.com/api-integrations (without slash) and https://us.springverify.com/api-integrations/ as different website. and it is happening for other internal pages too. So what i want is even if the URL is typed or directed to https://us.springverify.com/api-integrations (without a trailing slash) the URL should redirect to https://us.springverify.com/api-integrations/ (with trailing slashes). How do I achieve this with Cloudfront. please let me know if you need any other information.

推荐答案

这在 S3 和 Route53 中是不可能实现的.S3只是"一个数据库,您可以在其中通过键访问对象.例如,在您的静态网站示例中,每个页面都是一个 S3 对象,它有一个对象键,例如S3://my-bucket/pages/my-page

This is not possible to achieve with S3 and Route53. S3 is 'just' a database where you access objects via keys. For example, in your static websites example, each page is an S3 object, which has an object key, e.g. S3://my-bucket/pages/my-page

您可以使用 CloudFront 函数或 Lambda@Edge 实现类似的目标.如果您提供有关您想要实现的目标的更多信息,我可能会进一步帮助您.

You could potentially achieve something like this with CloudFront Functions or Lambda@Edge. If you provide more info on what you want to achieve, I will probably be able to help you further.

更新(在 OP 澄清之后):

我不确定这对 SEO 有多大帮助,但您可以使用 CloudFront 函数为 CF 分配收到的每个请求添加尾部斜杠.为此,您应该:

I am not sure about how helpful this is for the SEO, but you can use CloudFront Functions to add a trailing slash to each request you CF distribution receives. To do that you should:

  1. 使用事件类型创建 CF 函数:查看者请求.请参阅 AWS 文档此处了解如何执行此操作.
  2. 添加所需的 URL 重写(例如附加/")业务逻辑.请参阅下面的示例.
  1. Create a CF function with an event type: viewer request. See the AWS docs here for how to do that.
  2. Add the desired URL rewrite (e.g. append "/") business logic. See an example how to do that below.

     function handler(event) {
        var request = event.request;
        var uri = request.uri;
        
        if (!uri.endsWith('/')) {
            request.uri += '/';
        } 
       
        return request;
    }

最好的,斯蒂芬

这篇关于如何重定向内部 URL 以在 s3 网站中以斜杠结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆