As a website owner, you might find there are times when you want to move content from one URL to another. For example, you might revamp the overall site structure for your e-commerce store and haul everything under /clothes/shirts/ to more specific URLs, like /clothes/shirts/blouses and /clothes/shirts/knit-tops. Or you might swap out yourwebsite.com/blog for another URL like blog.yourwebsite.com. This is a situation when you would need to consider adding a 301 redirect to your site’s htaccess file.
When such changes are made to the site structure or URL, visitors are commonly met with a 404 – Page Not Found screen. However, it’s possible to smooth out the user experience and redirect an old URL to the new URL where the content is now hosted.
A redirect is simply a way to send users and search engines to a different URL than the one that was requested. This way, even if your old URLs are indexed on major search engines, people can see the new URL(s) where your content now actually lives.
Using our e-commerce example above, you could redirect visitors attempting to access yourwebsite.com/clothes/shirts/item-id-1457 to yourwebsite.com/clothes/shirts/blouses/item-id-1457, instead of serving a 404 status code and leading users to a dead end.
There are multiple types of redirects, but they can all be broken up into two main categories: server-side and client-side.
With a server-side redirect, the server sends a three-digit HTTP status code (starting with 3) when a request for a URL is made. The server determines what URL users and search engines should be directed to.
These are the most commonly used server-side redirects:
With client-side redirects, on the other hand, the browser handles redirection. There are two main types of client-side redirects: meta refreshes and JavaScript redirects.
Note that client-side redirects are typically not recommended for several reasons, namely:
All of that being said, it’s still useful to know about client-side redirects and how they work in contrast to server-side redirects.
One more redirect worth mentioning is the meta refresh, which is a client-side redirect that’s carried out on the page level, as opposed to the server level. If you’ve ever encountered a screen displaying a countdown and some text that read along the lines of, “If you are not redirected to so-and-so page in 10 seconds, click here,” you were looking at a meta refresh.
The code for a meta refresh might look something like this:
<http-equiv=”refresh” content=”0; url=https://yourwebsite.com/”>
It’s important to note that meta refreshes are typically not a recommended search engine optimization (SEO) practice, but it’s useful to know about them for specific circumstances.
JavaScript redirects instruct a browser to load a different URL than the one that was originally requested. The following is an example of what a JavaScript redirect might look like if you were sending users to https://www.yourwebsite.com/:
<script>window.location.replace(“https://www.yourwebsite.com/”);</script>
As discussed above, the 301 redirect indicates to browsers and search engines that the page has moved permanently to a new URL, and that the content from the old URL can be found at the new location.
Keep in mind that selecting the right type of redirect is crucial to maintain SEO performance and ensure that changes to site structure or content location don’t affect search engines’ ability to rank your site and your users’ ability to reach the correct pages on your website. The 301 redirect is recommended whenever you’ve permanently changed the URL of a website or webpage, because then the authority and relevance of your previous URL carry over to the new URL.
It’s best to use a 301 redirect in the following circumstances:
Whenever you change a URL permanently, whether it’s for a specific page or the domain name for your entire site, it’s important to ensure that your SEO performance and search engine rankings are affected as little as possible.
If you don’t use a 301 redirect — and instead resort to something like a meta refresh or simple JavaScript redirect — search engines and site users won’t be able to easily determine where the content was moved to. Your new URL won’t have enough relevance and authority to show up in search engine results, and site users will become frustrated as they’ll most likely see a 404 – Page Not Found screen. This will end up negatively affecting your site’s SEO rankings.
A server-side 301 redirect is the one you want to use when you’ve permanently changed your URL, as it will lead straight to the new location where your content is hosted.
There are a few different methods for adding a 301 redirect to your site. The best method for you will depend on variables like the individual site, how it’s set up, and what content management system (CMS) is being used.
The first method is to add a 301 redirect is to directly edit .htaccess, or the Hypertext Access file, which is a configuration text file that’s located on an Apache web server. To do this, you have to log into your server over FTP, make your edits, and re-upload the changed file(s) each time you want to add a new redirect.
Keep in mind that making direct edits to .htaccess is only possible if you’re using an Apache web server. The rules for handling redirects on an Nginx server are quite a bit different and require extensive administrative knowledge in order to modify server configuration.
(You may have noticed the .htaccess file in other content management systems as well — such as WordPress, Drupal, or Joomla — but the editing process varies from CMS to CMS. We’ll explore how to edit .htaccess in CPanel and WordPress later in this article.)
In the .htaccess file, you can specify rules for the server. Certain rules can redirect users to another site URL, and others can permit or forbid access to a user based on their IP address. The following are some common rules you can deploy to implement a 301 redirect for your website:
If you’ve changed the URL of your entire website, use:
Redirect 301 / http://www.yourwebsite.com/
If you’ve only changed the URL of a single page on your website, use:
Redirect 301 /about-page.php http://www.yourwebsite.com/about-page.html
If you want to redirect a particular subfolder to the main site, use:
Redirect 301 /blog http://www.blog.yourwebsite.com/
If you want to redirect the entire website to a particular subfolder, use:
Redirect 301 / http://www.yourwebsite.com/subfolder/
cPanel is an online graphical interface based on Linux. Many web administrators use the tool as a control panel to manage websites and servers. If you use cPanel for website and server management, here’s how you can add a 301 redirect via cPanel:
CPanel Add Redirect screen. Image source: Namecheap
There are two main options for adding a 301 redirect via WordPress:
To add a 301 redirect with plain PHP, you can use something like this:
<?php
// main_website.php
header(“HTTP/1.1 301 Moved Permanently”);
header(“Location: http://www.yourwebsite.com/new-URL”);
?>
Make sure you add the “HTTP/1.1 301 Moved Permanently” header, as PHP defaults to a 302 redirect if you don’t specify which type of redirect you want.
If you’re using WordPress’s built-in redirection function, you’d use the following:
wp_redirect( “http://www.yourwebsite.com/new-URL”, 301 );
As with plain PHP, it’s critical to specify “301” in the headers of the built-in wp_redirect function, as WordPress will default to a 302 redirect.
You have a lot of different options when it comes to adding 301 redirects for your website. The best method for you will depend on the specifics of your current site setup and the various tools and platforms at your disposal, including your web server, CMS, etc. That being said, here are some general notes to keep in mind as you weigh your options:
Direct edits to the .htaccess file can become cumbersome and difficult to maintain.
Making direct edits to the Hypertext Access configuration file can become unmaintainable over time, and your changes can be difficult to keep track of. Keeping .htaccess organized and staying on top of the redirects you’ve added can prove to be a challenge the bigger your site gets and the more redirects you add.
Editing the .htaccess file directly is only possible if you’re using Apache web servers.
The most obvious downside to directly editing the .htaccess file is that it’s only possible if you use Apache web servers. Nginx has its own rules for defining redirects in the associated server configurations and requires the user to possess extensive knowledge of system administration.
Creating redirects with WordPress can also become unmaintainable over time.
Adding redirects with WordPress’s built-in wp_redirect function or with plain PHP is quite a bit easier than editing files on the server via FTP, but just like the latter method, this can also become hard to maintain as you add more redirects to your website. However, this may prove to be a fast, lightweight option if you:
A user-friendly tool like cPanel can make it easier to add new redirects and view existing ones.
cPanel is one of the easier and more user-friendly ways to add redirects, as it offers a clean, intuitive graphical user interface (GUI). It lets you add multiple types of redirects and includes a table of records containing all redirects you’ve added to your website in the past, making it easy to keep track of site structure and URL changes.
There are a lot of different ways to redirect site visitors and search engines to different URLs, but the 301 redirect is doubtlessly one of the most useful in any SEO expert or web administrator’s toolkit. To recap, the three most common ways you can edit site configurations to add 301 redirects are to:
The importance of using a 301 redirect when making permanent changes to the URL of a website or webpage can’t be understated. When you move existing content to a new URL, this new URL has not built up enough authority or relevance to rank in search engine results.
As a result, you must take care to add a 301 redirect to avoid undermining the authority and relevance built up for your previous URL while still sending search engines and site visitors to the location of the new content.