Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get query strings in my Amazon S3 static website?

I am hosting a static website on Amazon S3. Some of my client-side javascript parses the query strings to control the HTML. This works fine locally, but on the S3-hosted version, the query strings seem to get dropped from the request.

My motivation for using query strings is that I want to be able to pass state between pages based on what the user did on the previous page.

Is this approach possible? Did I violate the "static" requirement for S3 static websites?

I can't seem to find any mention of general query strings in the S3 docs, aside from the authentication stuff, which I don't think solves my problem.

like image 535
scott_ri Avatar asked Dec 23 '13 21:12

scott_ri


People also ask

Can Amazon S3 run a static website?

You can use Amazon S3 to host a static website. On a static website, individual webpages include static content. They might also contain client-side scripts. By contrast, a dynamic website relies on server-side processing, including server-side scripts, such as PHP, JSP, or ASP.NET.

Can we query S3?

Amazon S3 Select and Amazon S3 Glacier Select enable customers to run structured query language SQL queries directly on data stored in S3 and Amazon S3 Glacier. With S3 Select, you simply store your data on S3 and query using SQL statements to filter the contents of S3 objects, retrieving only the data that you need.

What is a query string website?

On the Internet, a querystring (also called an HTTP querystring) is part of the set of characters automatically input in the address bar of a dynamic Web site when a user makes a request for information according to certain criteria.

Which S3 feature can be used to host static website?

To enable static website hosting Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that you want to enable static website hosting for. Choose Properties. Under Static website hosting, choose Edit.


2 Answers

If you are using index documents, where this...

http://bucket.example.com/foo

...actually displays the document at foo/index.html then your workaround will be to avoid the redirect that S3 does prior to displaying the index page.

For a link pointing to the page at /foo, you should actually create the link pointing to /foo/ instead (with the query string after the trailing slash; may look strange but it's quite valid). This is because when your browser requests a page that would result in an index document, but doesn't end in a trailing slash, S3 sends a redirect so that your browser has a trailing slash at the end of the URL. If not for this, relative links on that page would be referencing the wrong path.

The problem underlying the problem is that when S3 sends that redirect from /foo to /foo/, it strips the query string. They didn't have to design it that way, but that's what they did, and there does not appear to be a setting that modifies this behavior.

But if your link points directly to /foo/?some_thing=here&something_else=too instead of pointing to/foo?some_thing=here&something_else=too (no trailing slash before the ?) then the redirect is avoided and your query strings should stay intact.

like image 141
Michael - sqlbot Avatar answered Oct 16 '22 00:10

Michael - sqlbot


You could switch to using an anchor/hash rather than a query string. For example, instead of

http://someS3domain.com?variable=1

you could use:

http://someS3domain.com#variable=1

The big difference between an anchor/hash versus a query string is that the query string is sent to the server and the anchor/hash is not. In your case, since you're using S3 for static hosting, you don't need the query string to be sent to the server anyways.

In Javascript, you can get the value of the anchor/hash string using:

window.location.hash
like image 34
d2vid Avatar answered Oct 16 '22 00:10

d2vid