LoginSignup
9
13

More than 5 years have passed since last update.

AWS S3 でリダイレクトの設定

Posted at

S3 でホストしている静的 Web サイトのリニューアルを実施し、一部 URL が変更になりました。そこで、旧ページから新ページに 301 リダイレクトを設定したくなりました。

AWS マネージメントコンソールの日本語対応がされたので手順は日本語で書きます。

S3 の Management Console のバケットの一覧から「静的ウェブサイトホスティング」を選択し、「リダイレクトルールを編集する」を押します。

20150503112013.png

そこで出てきたテキストエリアに、リダイレクトルールの XML を書いていきます。具体的には下記のように、設定したい数だけ <RoutingRule> 要素を増やしていくことになります。

<RoutingRules>
    <RoutingRule>
        <Condition>
            <KeyPrefixEquals>old-page-1.html</KeyPrefixEquals>
        </Condition>
        <Redirect>
            <ReplaceKeyPrefixWith>new-page-1/</ReplaceKeyPrefixWith>
            <HttpRedirectCode>301</HttpRedirectCode>
        </Redirect>
    </RoutingRule>
    <RoutingRule>
        <Condition>
            <KeyPrefixEquals>old-page-2.html</KeyPrefixEquals>
        </Condition>
        <Redirect>
            <ReplaceKeyPrefixWith>new-page-2/</ReplaceKeyPrefixWith>
            <HttpRedirectCode>301</HttpRedirectCode>
        </Redirect>
    </RoutingRule>
</RoutingRules>

今回の案件では Middleman を使っていたので、 builder で生成させてみました。

routing-rules.xml.builder
mappings = {
  'old-page-1.html' => 'new-page-1/',
  'old-page-2.html' => 'new-page-2/',
}

xml.RoutingRules do |routing_rules|
  mappings.each do |from, to|
    routing_rules.RoutingRule do |routing_rule|
      routing_rule.Condition do |condition|
        condition.KeyPrefixEquals from
      end
      routing_rule.Redirect do |redirect|
        redirect.ReplaceKeyPrefixWith to
        redirect.HttpRedirectCode 301
      end
    end
  end
end

詳しくは下記に仕様があります。

賛否あるかとは思いますが、日本人のぼくにとって日本語で使えるツールやドキュメントが増えていくのは良いことですねー。

9
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
13