Search code examples
amazon-web-servicesamazon-s3http-redirectamazon-cloudfront

How to redirect internal URLs to end with trailing slashes with s3 websites


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

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.


Solution

  • 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

    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.

    UPDATE (after OP clarification):

    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. 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;
        }
    
    

    Best, Stefan