How to create a simple redirect from Blogspot to your new blog

I wanted to redirect from my old blog to a new one, but redirect to the same content you wanted to see.

This post is inspired by How to automatically redirect Blogger blog to another blog or website. If you're looking for something simple, you can take some inspiration from what I did.

Step 0: Edit template #

Go to Blogger template (in settings) and click edit HTML.

Step 1: Script #

Use plain old script tag inline.

<script></script>

You may want to put it somewhere in the <head> </head> so it will probably look like:

<!-- lots of things you should ignore -->
<head>
<script></script>
<!-- lots of things you should ignore -->
</head>

Step 2: JavaScript #

Now you can start writing JS inside of it.

<script>
console.log(window.location)
</script>

Step 3: Redirect #

    window.location="https://kula.blog/"; // your your new address here

If you don't care about redirecting your posts, then you're done!

Step 4: Redirect to posts #

This may be different for you. In my case, after exporting the posts, I've moved them to a new location. They've also lost the dates and .html from the address. Now I had to transform each address to match that. I'm going to assume your situation is different, but you might get something from looking at how it can be done.

In my case, https://krzychukula.blogspot.com/2014/07/note-to-myself-learn-videogame.html had to redirect to https://kula.blog/oldposts/note-to-myself-learn-videogame/.

Pathname #

Now to make it simpler, we can use window.location.pathname to get just /2014/07/note-to-myself-learn-videogame.html. Less work later.

Check if it's a post #

I don't care about pages that are not posts. For that, I check if the pathname ends with .html.

var newAddress = 'https://kula.blog/';
if (window.location.pathname.indexOf('.html') > -1) {
// figure our newAddress for each posts
}
// redirect to 'https://kula.blog/'
window.location=newAddress;

I'm using var to support even the oldest browsers, and it's only a simple script, so it should be fine.

Revomed date and file suffix #

var newAddress = 'https://kula.blog/';
if (window.location.pathname.indexOf('.html') > -1) {
var pathname = window.location.pathname;

// will match `/2014/07/`
var regex = /\/\d+\/\d+\//g;
// replace with empty string
var filename = pathname.replace(regex, '');

// replace with empty string
var file = filename.replace('.html', '');

newAddress = 'https://kula.blog/oldposts/' + file;
// newAddress is now `https://kula.blog/oldposts/note-to-myself-learn-videogame`
}
// redirect to the newAddress
window.location=newAddress;

Final script #

Please notice that the Blogger editor escapes some characters for you. I'm posting the final escaped result to make sure you know that's ok.

<script>
var newAddress = &quot;https://kula.blog/&quot;
if (window.location.pathname.indexOf(&quot;.html&quot;) &gt; -1) {
var pathname = window.location.pathname;
var regex = /\/\d+\/\d+\//g;
var filename = pathname.replace(regex, &#39;&#39;);
var file = filename.replace(&#39;.html&#39;, &#39;&#39;);

newAddress = &quot;https://kula.blog/oldposts/&quot; + file;
}

window.location=newAddress;
</script>


Share on Hacker News
Share on LinkedIn


← Home


Want to learn more?

Sign up to get a digest of my articles and interesting links via email every month.

* indicates required

Please select all the ways you would like to hear from Krzysztof Kula:

You can unsubscribe at any time by clicking the link in the footer of my emails.

I use Mailchimp as a marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp's privacy practices here.