cscan
cscan

Reputation: 3840

S3 redirect requests

I'm trying to set up static web hosting with redirect rules. In my application, any requests with the prefix api/ (http://web.company.com/api) should be redirected to another host with the same prefix (http://api.company.com/api). I have set up my routing rules as follows:

<RoutingRules>
  <RoutingRule>
    <Condition>
      <KeyPrefixEquals>api/</KeyPrefixEquals>
    </Condition>
    <Redirect>
      <HostName>api.company.com</HostName>
    </Redirect>
  </RoutingRule>
</RoutingRules> 

But the requests aren't being redirected properly. There are two different errors in the console:

XMLHttpRequest cannot load http://api.company.com/api/logout. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://bucket.s3-website-us-west-2.amazonaws.com' is therefore not allowed access.

angular.js:10661 POST http://bucket.s3-website-us-west-2.amazonaws.com/api/oauth/token?grant_type=client_credentials 405 (Method Not Allowed)

Since one of the issues is a CORS problem here is my CORS configuration:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>http://*</AllowedOrigin>
        <AllowedOrigin>https://*</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

It looks like the requests to /api/something are being redirected (but refused by the browser) while /api/something/else are not being redirected. What am I doing incorrectly?

Upvotes: 1

Views: 1038

Answers (1)

Paul Tyler
Paul Tyler

Reputation: 113

I ran into a similar problem. I set up two static websites. One is the root domain. The second is a subdomain. We needed a redirect from the root to the subdomain. We got it work with syntax like this:

<RoutingRules>
  <RoutingRule>
    <Condition>
      <KeyPrefixEquals>api/</KeyPrefixEquals>
    </Condition>
    <Redirect>
      <HostName>api.company.com</HostName>
      <Protocol>https</Protocol>
      <ReplaceKeyWith>index.html</ReplaceKeyWith>
    </Redirect>
  </RoutingRule>
</RoutingRules> 

Documentation, though not really clear, is found here: http://docs.aws.amazon.com/AmazonS3/latest/dev/HowDoIWebsiteConfiguration.html#configure-bucket-as-website-routing-rule-syntax

Upvotes: 2

Related Questions