01 March 2021

What is a Redirect? How to Edit 301 Redirect in htaccess

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. 

What Is a 301 Redirect?

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.

Types of Redirects

There are multiple types of redirects, but they can all be broken up into two main categories: server-side and client-side. 

Server-Side Redirect

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:

  • 301 Moved Permanently. This redirect indicates to search engines and browsers alike 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. This is the redirect we’ll focus on in the rest of this article.
  • 302 Found or Moved Temporarily. This redirect indicates that the URL change is temporary, and there’s a high probability that search engines will still lead users to your old URL.
  • 303 See Other. This redirect prevents visitors from being able to re-submit forms when they click the Back button in their browser. This type of redirect should only be used to handle form submissions. 
  • 307 Moved Temporarily. This is the HTTP 1.1 successor of the 302 redirect.
  • 308 Permanent Redirect. This is the permanent version of the 307 redirect. We still don’t have enough data on how search engines interpret the 308 redirect, so it’s recommended that users implement a 301 redirect to indicate permanently moved content. 

Client-Side Redirect

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:

  • Insufficient browser support. Some browsers don’t support client-side redirects.
  • Search engines may not be able to pick up on them. For search engines to pick up on a JavaScript redirect, they need to execute your website or webpage. Due to limited resources for JavaScript execution, it might take quite a bit of time before a search engine becomes aware of a JavaScript redirect. As for meta refreshes, search engines are likely to treat them as 301 redirects if the refresh time is set to 0. 
  • Issues with the Back button. Client-side redirects might break the Back button. If the user clicks the Back button, the client-side redirect would happen again and the page would be refreshed, annoying the user and perhaps encouraging them to leave your website!

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.

Meta Refreshes

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 Redirect

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>

What Is a 301 Redirect?

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. 

When Should You Use a 301 Redirect?

It’s best to use a 301 redirect in the following circumstances:

  • You’re switching over to HTTPS from HTTP (for example: http://www.yourwebsite.com to https://www.yourwebsite.com).
  • You’ve changed domain names (oldURL.com to newURL.com).
  • You’ve changed the URL for a single page.
  • You’ve changed the URL of a subfolder (like yourwebsite.com/clothing to yourwebsite.com/womens-clothing).
  • You want to move a subdomain to a subfolder (for example: dashboard.yourwebsite.com to yourwebsite.com/dashboard).
  • You want to group a number of related webpages under a single umbrella.

How 301 Redirects Affect SEO Performance

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.

How to Create a 301 Redirect

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.

Edit .htaccess File Directly via FTP

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:

Redirect to a New Site 

If you’ve changed the URL of your entire website, use:

Redirect 301 / http://www.yourwebsite.com/

Redirect a Single Page

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

Redirect a Subfolder to a New Site

If you want to redirect a particular subfolder to the main site, use: 

Redirect 301 /blog http://www.blog.yourwebsite.com/

Redirect a Site to a Particular Subfolder

If you want to redirect the entire website to a particular subfolder, use:

Redirect 301 / http://www.yourwebsite.com/subfolder/

Edit .htaccess in CPanel

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:

Add a permanent 301 redirect to your htaccess file in cPanel.

CPanel Add Redirect screen. Image source: Namecheap

  1. Log into cPanel.
  2. Scroll down to the section titled “Domains.” Under “Domains,” click on “Redirects.”
  3. You should see a screen titled “Add Redirect.” To implement a 301 redirect, select “Permanent (301)” in the dropdown box titled “Type.”
  4. From the dropdown menu titled “http://(www.)?”, select your old website URL (the one you’re going to be redirecting from).   
    1. If you’re redirecting your entire site, you can stop there. 
    2. If you want to redirect only a single page, add the subdirectory and page after the forward slash in the next dropdown menu (you’ll see that it’s marked by a forward slash).
  5. In the “Redirects to” box, enter the site or page URL that you want the old site or page to redirect to.
  6. In the radio menu titled “www. redirection”, you can select under what circumstances your old URL should be redirected to your new URL. Your options are to redirect only URLs that start with “www.”, to never redirect URLs that start with “www.”, or to redirect with or without a “www.” at the beginning of the URL. We recommend “Redirect with or without www.”, but ultimately the selection is up to you and should be based on site specifics. 
  7. Once you’re done with the steps above, the last step is to ensure that the redirection was set up properly. You can do this by scrolling down to the section titled “Current Redirects” — which contains a table of records displaying all redirects across your site — and checking whether the information you just entered has been stored in the table.  

Edit .htaccess in WordPress

There are two main options for adding a 301 redirect via WordPress:

  1. Alter file headers with plain PHP.
  2. Use WordPress’s built-in wp_redirect function.

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. 

Which Method Is Right for You?

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:

  • Are a WordPress developer who doesn’t intend to add a lot of redirects or site structure changes
  • Don’t have access to cPanel

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. 

Conclusion

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:

  • Edit the .htaccess file directly by logging into your server via FTP.
  • Use WordPress’s built-in wp_redirect function or alter the appropriate file headers with plain PHP code.
  • Use cPanel and go to Domains >> Redirects >> Add Redirect.

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.

Book a Demo

Take a deep dive into WatchTowerHQ with a member of our Customer Success Team.

Sign up here