A Ruby script to generate simple Amazon S3 Redirection Rules.
- Nokogiri
ruby s3-routes-generator.rb input.txt
The generated XML is printed to STDOUT.
- For
targetpath, if it ends with/, thenindex.htmlwill be appended. - For
sourceandtargetpath, the leading/will be removed, because S3's object key does not begin with/.
One rule per line. Each rule is constructed by source and target path, spearated by at least 1 space.
Please not that rules are matched using KeyPrefixEquals, and is First-In-First-Out, so if you have multiple redirections from same prefixes (typically /dir and /dir/subdir), please put the longer one before the shorter one, i.e. Depth-First.
/home /
/products/iphone/specs.html /iphone/specs.html
/products/iphone/ /iphone/
/products/ipad/accessories/ /ipad/accessories.html
/products/ipad/ /ipad/
/products /
<?xml version="1.0"?>
<RoutingRules>
<RoutingRule>
<Condition>
<KeyPrefixEquals>home</KeyPrefixEquals>
</Condition>
<Redirect>
<ReplaceKeyWith>index.html</ReplaceKeyWith>
</Redirect>
</RoutingRule>
<RoutingRule>
<Condition>
<KeyPrefixEquals>products/iphone/specs.html</KeyPrefixEquals>
</Condition>
<Redirect>
<ReplaceKeyWith>iphone/specs.html</ReplaceKeyWith>
</Redirect>
</RoutingRule>
<RoutingRule>
<Condition>
<KeyPrefixEquals>products/iphone/</KeyPrefixEquals>
</Condition>
<Redirect>
<ReplaceKeyWith>iphone/index.html</ReplaceKeyWith>
</Redirect>
</RoutingRule>
<RoutingRule>
<Condition>
<KeyPrefixEquals>products/ipad/accessories/</KeyPrefixEquals>
</Condition>
<Redirect>
<ReplaceKeyWith>ipad/accessories.html</ReplaceKeyWith>
</Redirect>
</RoutingRule>
<RoutingRule>
<Condition>
<KeyPrefixEquals>products/ipad/</KeyPrefixEquals>
</Condition>
<Redirect>
<ReplaceKeyWith>ipad/index.html</ReplaceKeyWith>
</Redirect>
</RoutingRule>
<RoutingRule>
<Condition>
<KeyPrefixEquals>products</KeyPrefixEquals>
</Condition>
<Redirect>
<ReplaceKeyWith>index.html</ReplaceKeyWith>
</Redirect>
</RoutingRule>
</RoutingRules>It assumes that:
- Website Hosting is enabled.
- Index Document is set to
index.html.
None. I release this script to Public Domain.
Is it possible to detect user agent from the request and redirect based on the user agent on amazon s3 static hosting?