Create a redirection site using Amazon S3
Previously I chose to move all my posts from my old blog at litemedia.info to this blog, and shutdown that site. It was mainly because that site was malfunctioning and I wanted to make the content more accessible on mobile.
After I migrated every blog post, I created the following Web.config in order to redirect all the old URLs to the new domain.
This works great, the Azure website is redirecting all the old URLs to their target URLs in the new website. However I'm now paying $10 a month for a bunch of HTTP 301, and that doesn't really seem worth it. After doing some thinking I made the conclusion that there are much easier solutions for doing 301 redirects rather than using a Web.config in an Azure website.
You can host a website from an S3 bucket and it is just a fraction of the cost of an Azure website. By serving content with the HTTP header
x-amz-website-redirect-location you can individually redirect each old URL to a new URL1.
Create a new bucket in Amazon S3 and name it exactly like your domain you want to redirect from. Make sure that you set both index and error in the website settings. By redirecting the error page to the new domain, you will be able to have a catch all scenario where all URLs you have not redirected individually will end up at the root of the new webpage.
You can choose to redirect the whole website at this point, but I would like to customize redirection for individual URLs and redirecting all requests to a target URL is not what I intend to do.
Instead I want to create a file for each old URL and upload that file with the HTTP header that redirects to the new URL that I specify. I will do this by using the AWS Command Line Interface.
touch asserting-with-exceptions aws s3 cp asserting-with-exceptions s3://litemedia.info/ --website-redirect //blog.mikaellundin.name/2009/04/21/asserting-with-exceptions.html
First I create an empty file called
asserting-with-exceptions which was the URL for this post on my previous site. Then I upload this file with the HTTP header to redirect to the new URL. Luckily I had a quite flat URL hierarchy at litemedia.info.
After making this file public, I can test it by browsing to the bucket URL http://litemedia.info.s3-website-eu-west-1.amazonaws.com/asserting-with-exceptions and verify that I get redirected to http://blog.mikaellundin.name/2009/04/21/asserting-with-exceptions.html.
I don't want to do this manually to all 250 URLs so what I did was transform the XML with the URLs that I had, using the following XSL.
<xsl:template match="/"> <xsl:apply-templates select="configuration/location" /> </xsl:template>
<xsl:template match="location"> touch <xsl:value-of select="@path" /> aws s3 cp <xsl:value-of select="@path" /> s3://litemedia.info/ --website-redirect <xsl:value-of select="system.webServer/httpRedirect/@destination" /> </xsl:template> </xsl:stylesheet>
And with some minor adjustments turned the data with the URLs into a runnable script.
export AWSACCESSKEYID=LDS4LSK4JLSKGÖSL export AWSSECRETACCESSKEY=iJSKiLSvmksLSK/OPlskvmosoimfsldklse export AWSDEFAULTREGION=eu-west-1
touch index aws s3 cp index s3://litemedia.info/ --website-redirect //blog.mikaellundin.name/ touch error aws s3 cp error s3://litemedia.info/ --website-redirect //blog.mikaellundin.name/ touch asserting-with-exceptions aws s3 cp asserting-with-exceptions s3://litemedia.info/ --website-redirect //blog.mikaellundin.name/2009/04/21/asserting-with-exceptions.html
.. and about 250 more URLs ..
What is interesting here is that I choose to redirect also index and error, meaning I will catch the root address
http://litemedia.info and also any unknown URL with error.
Move the domain to Amazon Route 53
After you've verified that the redirection is working with the bucket url2 you're ready to point the domain to that bucket. If the domain is a subdomain then you can just create a CNAME record to the bucket and it will work all magically.
In this zone you shall create an alias that will point to your bucket.
Now Amazon Route 53 knows that requests coming to litemedia.info should be routed to the bucket. In the last step we need to tell our domain registrar to use the Amazon nameservers.
You find the nameservers by inspecting the hosted zone that you've created.
I have this domain registered with a registrar called Binero and they allow me to change nameservers from their own to Amazon by a form.
And after 24 hours the change had gone through and I would get the Amazon nameserver when querying with nslookup.
When testing it I can now see that every URL on litemedia.info is redirecting to the corresponding URL on blog.mikaellundin.name.
My bucket URL is http://litemedia.info.s3-website-eu-west-1.amazonaws.com ↩