What are 301 Redirects?

A 301 redirect is a permanent redirect of a web address to a different one. In this article, we’ll explain the technical background and show you how you can set up a 301 redirect using htaccess.

Why are 301 redirects needed?

The 301 redirect is the best-known type of redirection. The number 301 is based on the HTTP status code. While the status code 404 shows that the requested resource can’t be found, 300 codes stand for redirects. Generally, you use a 301 redirect to avoid 404 errors.

Here’s an overview of the most important HTTP status codes:

HTTP status code Description
200 OK The server found the resource at the requested address and can deliver it back.
301 Moved permanently The resource at the requested address has been permanently moved and the server has returned it from there. The old address is no longer valid.
302 Found The resource at the requested address has been temporarily moved and the server has returned it from there. The old address will be valid again at some point in the future.
303 See other Similar to 302, however, when opening the temporary address only GET-requests are allowed.
307 Temporary redirect Similar to 302, however, different to 303 in that the client may change the HTTP methods (GET, POST, PUT, etc.) when opening the temporary address.
404 Not found The server can’t find any resources from the requested address.
### 301 redirects for content migration

To locate content, or resources, on the web, web addresses are used. When content migration occurs, the address where the content can be found changes. The content itself, however, does not. This is often the case when switching from HTTP to HTTPS or when moving a site from one domain to another. An address may also change when optimizing the link structure for search engines. In all these cases, you don’t want users who are trying to access the content with the old address to land on an empty page.

If a server can’t find a resource at a given address, it will return a HTTP 404 Not Found error. This type of error tends to annoy users since they aren’t able to access the webpage they want to. If the address is opened by a web crawler instead of a person, 404 errors can lead to the de-indexing of that resource. Website operators try to avoid both instances as best they can.

### 301 redirects for search engine optimization

Apart from changing the addresses of web content, 301 redirects are also an essential part of search engine optimization (SEO) and online marketing. 301 redirects are used to create search-friendly URLs and canonical URLs. URL shorteners are often used on social media and are also derived from 301 redirects.

Why are 301 redirects so important for SEO? Unlike 302, 303 and 307 redirects, with a 301 redirect the “link juice” of the original address is transferred to the new one. If a search engine has indexed this content and rated it positively, the address won’t lose its rating when replaced with the 301 redirect. 301 redirects also make complex SEO strategies such as multiple domain strategy possible.

How do 301 redirects work?

301 redirects come into use during exchanges between web servers and clients. The server tells the client that the requested resource is no longer available at the given web address (URL) and provides it with the new valid address. Both a person’s browser as well as a search engine bot can be considered clients.

  1. The client attempts to access the resource at the URL.
  2. The server answers with the status code “301 moved permanently” and gives the new URL as a HTTP location header.
  3. The client saves the new URL in its cache, meaning any further attempts to access the old URL will automatically direct to the new one.
  4. The client then attempts to access the resource from the new URL.

This process can be seen using the popular cURL network tool. You can open the IONOS homepage, http://ionos.com, and use the option –head, to display the HTTP response header:

curl --head http://ionos.ca
bash

The IONOS server answers with the HTTP-Status-Code “301 Moved Permanently” and the location header contains the new address https://www.ionos.com/. As you can see, the homepage is under the www subdomain and uses the HTTPS protocol. Next, you can tell cURL to follow the 301 redirect using the option –location.

curl --head --location http://ionos.ca
bash

cURL will now show the header for two connections. The second one answers the server with the status code “200 OK”. The resource was found, and the content is shared in the response body. The same process occurs in the background of your browser when you enter an address in there.

### Setting redirect 301 with htaccess

We’ve already taken a look at how 301 redirects work between clients and servers. But how do you set up 301 redirects on a server? There are different methods to do this but one of the most common ways is to set up a redirect in the htaccess file of the Apache web server. In order for this to work, the corresponding Apache module needs to be open:

Directives Apache module Description*
Redirect mod_alias Simple redirect
Rewrite mod_rewrite Complex redirect
### Simple 301 redirects in the htaccess file

Let’s take a look at how simple 301 redirects can be set up in the htaccess file. To do this, use the redirect directive from the mod_alias module. These are suitable when you want to redirect a URL on the server to another URL. The template is as follows:

Redirect 301 "/source_path" "target_URL"
apache

Next, redirect the URL on the server to another internal URL. The forward slash at the beginning indicates the start of the URL path, which is the part that comes after the domain:

Redirect 301 "/old.html" "/new.html"
apache

To send it to an external URL instead, add the external URL as the target:

Redirect 301 "/old.html" "http://example.ca/new.html"
apache

Using the redirect directive, you can also define 301 redirects for entire indexes in the htaccess file. In this example, we’ll redirect all URLs with the “de” path component to a subdomain.

Redirect 301 "/de/" "http://de.example.ca/"
apache

Similarly, we can redirect all the URLs on a domain to a new domain:

Redirect 301 "/" "http://other.example.ca/"
apache

If you want to redirect individual addresses that have a similar pattern, you can use the RedirectMatch directive. This way you don’t have to note the redirects individually. The directive uses a standard expression to recognise patterns and extract from them.

RedirectMatch 301 regex URL
apache

Here we’ll show you a RedirectMatch entry which extracts the category and ID of an article from the GET parameters and then redirects them to the path.

RedirectMatch 301 "/article.php?cat=(.*)&id=(.*)" "/articles/$1/$2/"
apache

Imagine our domain is example.ca. The URL http://example.com/article.php?cat=web&id=42 would be redirected to http://example.com/articles//web/42/.

### Complex 301 redirects in the htaccess file

To create a 301 redirect for advanced redirects you can use the rewrite directive from the Apache rewrite module mod_rewrite. This allows you to define and request redirect conditions, set and read environmental variables, and define redirect rules with regular terms.

With mod_rewrite, it’s possible to chain redirect rules. The module will deliver something similar to a mini-programming language to reform URLs and carry out redirects. However, this is something you must be careful with, because if you use it incorrectly, you can cause infinite loops.

The rewrite directive begins with the target “RewriteEngine on” followed by the optional RewriteCond conditions. After that comes one or more multiple RewriteRule redirect rule(s). How the rewrite conditions and rules work can be changed using Flags, which are essentially different options. To set up a 301 redirect, a rule must contain the flag L,R=301:

RewriteEngine on
RewriteCond TestString CondPattern [flags]
RewriteRule URL_path Substitution [L,R=301]
apache

Within the redirect rules and conditions, you can access multiple header and server variables from the form %{HTTP_HOST}, %{REQUEST_URI} etc. These can be linked by operators and edited as functions. This means almost all desired redirects can be carried out, even if the syntax isn’t exactly intuitive.

Let’s have a look at an example of a 301 redirect using the mod_rewrite. We want to force canonical URLs to avoid duplicate content on our website. This means that all URLs begin with HTTPS and www., regardless of how users open the content.

We can define two redirect conditions, which can be linked using the OR flag. The first condition checks whether HTTPS is off, and the second condition checks whether the domain is missing the prefix “www.” With the NC flag, we can switch off the difference between uppercase and lowercase letters. If one of the conditions is true, we redirect it to the https-www. Version of the URL. To do this, we use the common phrase (.*), which all URLs access. To enter it behind the domain, use $1:

RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^example\.ca [NC]
RewriteRule (.*) https://www.example.ca/$1 [L,R=301]
apache

What are the problems and risks associated with using 301 redirects?

A 301 redirect is a common tool for web development and SEO. However, you must be careful. If redirects are used incorrectly, they can cause a lot of problems, including server crashes and a drop in search engine rankings.

### Damaging the htaccess file during manual editing

The htaccess file configures the Apache web server at the index level. Even the smallest incorrect change to the htaccess file can easily lead to websites being offline. This leads to the worrisome server error 500 or the White Screen of Death on WordPress.

To avoid this, it is best to make a copy of the htaccess file when editing it. This can easily be done from the command bar. Simply switch to the public web index of the site and use the cp command:

cp -a .htaccess _htaccess
bash

If any changes made in the htaccess file cause errors, it can be restored to its original version:

cp -a _htaccess .htaccess
bash
### Chained redirects and redirect loops

Redirects are useful but can cost a complete request-response cycle. If chained redirects are used, users have to wait, which can lead to an increased bounce rate. Search engines also rate sites negatively if they carry out multiple redirects in a row. As a rule of thumb, no more than three redirects should be set up.

The worst thing that could happen would be a redirect loop where two addresses constantly redirect to one another. This appears to users as the browser error ERR_TOO_MANY_REDIRECTS, meaning that the resource they want to access is no longer available. You should try and test redirects using cURL to make sure that you get the result you want.

### Possible cache poisoning when using 301 redirects

Incorrectly using 301 redirects can be the source of many headaches later on. Unlike temporary 302, 303 and 307 redirects, the 301 redirect tells the client to save the new URL in its cache. If you want to go back to the original URL, the client won’t be informed. Instead, it will keep trying to connect to the new URL. To counteract this, you can use temporary redirects or set the cache control value to the appropriate value.

Let’s set the cache control header to an hour. This ensures that later changes to the redirect target will be received by all clients.

ExpiresActive on
ExpiresDefault "access plus 1 hour"
Redirect 301 / https://www.example.ca/
apache

If you use advanced redirects with rewrite directives, there’s a trick to setting the cache control header. We can define the environment variable limitcache as part of the 301 redirect rule. Finally, we can set the header, using the environment variable as a condition. This ensures that when redirecting, the correct header is always used:

RewriteRule ^.* http://www.example.ca/ [L,R=301,E=limitcache:1]
Header always set Cache-Control "max-age=3600" env=limitcache
apache
Tip

Followed a 301 redirect and now your browser is refusing to access the original address? Simply attach a query string with ?foo=bar to the old address and force the browser to load the URL.

We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.