Redirecting a Web Page

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Today we are going to look at the concept of redirecting a web page. We can use redirects for several purposes. Being but not limited too:
There are many ways of doing this; we will be covering a couple of examples in this article.
Note: There are more ways of doing this than described in this article alone.
In specific redirects, it's good to understand the different HTTP Response Codes; these are codes the browser sends back on a request. All response codes have a particular meaning:
200: Means the page loaded, no issues found404: The page could not be found2XX: There is a wide range of 200 codes which all mean success3XX: For redirects however we are interested in the several 300 codes:301: Moved Permanently, the page is moved to another page302: Moved Temporary, The page is moved only temporaryThis is by far the easiest and one of the most relaxed options. I do use this to automatic redirect after x seconds very often.
We place a meta tag in the head of our document like this:
<meta http-equiv="refresh" content="15; URL='http://ebsite.com/homepage'" />
As you can see, we can define the content attribute, which tells us to wait 15 seconds before redirecting to the homepage.
We can, however, use 0 as an argument, and it will directly redirect.
Being said, this is the easiest way; there are some browsers that do not like this method. Meaning; the user can experience a flash before it redirects to the end page.
Another way to redirect a page is using JavaScript.
It's as easy as using the window object.
window.location = 'http://website.com/homepage';
There are multiple ways of doing this in JavaScript:
window.location = 'http://website.com/home';
window.location.href = 'http://website.com/home';
window.location.assign('http://website.com/home');
window.location.replace('http://website.com/home');
Ofcourse we can wrap this in a 15 second timeout if we would like to:
setTimeout(function() {
window.location = 'http://website.com/homepage';
}, 15000);
A way that is mainly used for more permanent redirects or moving a whole subdomain or even domain is using the .htaccess file.
This file allows us to send the HTTP Status Codes as well.
Redirect 301 / http://xyz.com
Redirect 302 / http://xyz.com
Being said, we can match a whole section, for instance:
RedirectMatch 301 /blog(.*) http://xyz.com$1
This will redirect everything that starts with blog to xyz.com/blog/query.
Even PHP can easily redirect to another web page:
<?php
header('Location: http://xyz.com', true, 301);
exit;
?>
As you can see, we can define the HTTP Status Code again, if you leave this empty, the default value 302 will be used.
There are many more ways of redirecting; I've used all of the above in one way or another. It depends on what your software is built on, and what goal it should serve.
Other redirect methods:
Let me know in the comments what your preferred way of redirecting is.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter