SEO

How to create 301 Redirects with .htaccess

December 12, 2022
7 min of reading
How to create 301 Redirects with .htaccess
Connect with us

Join our list and receive exclusive content

A 301 redirect is a permanent redirect from one URL to another. This is commonly used when a website has moved to a new domain or a page has been permanently moved to a new location.

A 301 redirect is helpful because it tells search engines that the old URL has been permanently moved to the new URL. This ensures that the search engines will update their indexes to reflect the new location of the content, and it also helps to preserve any search engine rankings that the old URL may have had.

When you use a 301 redirect, it is important to use the correct code and to set up the redirect properly. This will ensure that the redirect is effective and that it does not cause any issues with your website or with search engines.

In general, you should use a 301 redirect when you are permanently moving a page or a website to a new location. This could be because you have changed your domain name, restructured your website, or for any other reason.

It is also a good idea to use a 301 redirect if you are merging two websites or if you are deleting a page from your website. This will help to ensure that any incoming links to the old URL will still work, and it will also help to preserve any search engine rankings that the old URL may have had.

A 301 redirect is a useful tool for managing the location of your website’s content and for maintaining the integrity of your website. By using a 301 redirect properly, you can ensure that your website remains accessible and that it continues to rank well in search engines.

To set up a 301 redirect, you can use a .htaccess file on your server. This is a configuration file that allows you to control how your website behaves, and it is often used to set up redirects.

What Is an .htaccess File?

An .htaccess file is a configuration file used by the Apache web server software. It is a simple text file that is placed on the server and contains instructions for the server on how to handle certain requests.

The .htaccess file is often used to control access to files and directories on the server, to set up custom error pages, and to set up URL redirects. It can also be used to configure various other aspects of the server and the website, such as caching, security, and performance.

The .htaccess file is called a “dotfile” because it begins with a period (.) and it is usually hidden from view in file listings. To create an .htaccess file, you can use a text editor like Notepad or TextEdit. Simply create a new file and save it as .htaccess (note the leading period). Then, you can add the code for your desired configuration and upload the file to your server.

It is important to use the .htaccess file carefully, as it can have a significant impact on your website’s performance and security. For example, a poorly configured .htaccess file can cause your website to become inaccessible or slow to load.

Other types of redirects

In addition to the 301 redirects, there are several other HTTP status codes that can be used for redirects. These include:

  • 302 Found (Temporary Redirect): This code indicates that the requested URL has been temporarily moved to a new location.
  • 303 See Other: This code indicates that the response to the request can be found under a different URL.
  • 307 Temporary Redirect: This code is similar to the 302 Found status code, but it specifies that the request method (e.g. GET, POST) should not be changed when redirecting the request.
  • 308 Permanent Redirect: This code is similar to the 301 Moved Permanently status code, but it specifies that the request method should not be changed when redirecting the request.

These status codes can be useful in different situations, depending on your specific needs. For example, you might use a 302 Found status code if you are temporarily moving a page or a website to a new location, or you might use a 303 See Other status code if you want to redirect a user to a different URL but you want them to use the same request method (e.g. GET or POST) as they did in their original request.

It is important to use the appropriate HTTP status code for your redirect, as this will ensure that the redirect is handled properly by the user’s browser and by search engines. In general, the 301 Moved Permanently and 308 Permanent Redirect codes are the most commonly used for permanent redirects, while the 302 Found and 307 Temporary Redirect codes are used for temporary redirects.

How to Do 301 Redirects with .htaccess Files on Apache

The .htaccess file must be placed on an Apache server in order for the redirect to work. If your website is hosted on a different type of server, you will need to use a different method for setting up a 301 redirect.

First of all, you need to locate the RewriteEngine and load it by using the following code:

<IfModule mod_rewrite.c>
RewriteEngine On
</IfModule>

The mod_rewrite module is a module in the Apache web server software that provides a powerful way to rewrite URLs. This allows you to control how Apache processes requests that match certain patterns, and it can be used to set up redirects, hide the underlying structure of your website, and implement various other types of URL manipulation.

The mod_rewrite module is enabled by default in Apache, but if it is not enabled, you can enable it by adding the following line to your Apache configuration file (usually httpd.conf or apache2.conf):

LoadModule rewrite_module modules/mod_rewrite.so

Once the mod_rewrite module is enabled, you can use the RewriteEngine directive in your .htaccess file to control how Apache processes requests that match certain patterns.

Here are some examples of how to set up a 301 redirect using a .htaccess file:

Redirecting a single page to a new location:

Redirect 301 /old-page.html http://www.example.com/new-page.html

Redirecting an entire site to a new domain:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^oldsite.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.oldsite.com [NC]
RewriteRule ^(.*)$ http://newsite.com/$1 [L,R=301,NC]

This code will redirect any request made to oldsite.com or www.oldsite.com to newsite.com. Replace oldsite.com and newsite.com with your own domain names.

Redirecting a specific subdirectory to a new location:

RewriteEngine on
RewriteRule ^old-subdirectory/(.*)$ http://www.example.com/new-subdirectory/$1 [R=301,L]

This code will redirect all requests for pages or resources in the “old-subdirectory” to the “http://www.example.com/new-location” URL.

Redirecting all traffic to a new domain, regardless of the page requested:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^oldsite.com [NC]
RewriteRule ^(.*)$ http://newsite.com/$1 [L,R=301]

Redirecting www to Non-www

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

Force HTTPS Redirects

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

You must have an SSL certificate installed on your website for this to work. Otherwise, you’ll get the cautionary “Not secure” message and your redirect won’t work.

These are just a few examples of how to set up a 301 redirect using a .htaccess file. There are many more options available, and the specific code you will need to use depends on your requirements.

Be sure to save the file as .htaccess (note the leading period) and upload it to the root directory of your old website. This will activate the redirect.

How to create 301 Redirects using WordPress

To create a 301 redirect using WordPress, you can use a plugin that adds redirect functionality to your website. There are many redirect plugins available for WordPress, but one popular option is the “Redirection” plugin. To use this plugin to create a 301 redirect:

  1. Install and activate the “Redirection” plugin on your WordPress website.
  2. Navigate to the “Tools > Redirection” page in your WordPress admin dashboard.
  3. In the “Source URL” field, enter the old URL that you want to redirect (e.g. “http://www.example.com/old-page/“).
  4. In the “Target URL” field, enter the new URL where you want to redirect the old URL (e.g. “http://www.example.com/new-page/“).
  5. Choose “301 (permanent)” from the “Type” dropdown menu.
  6. Click the “Add Redirect” button to save the redirect.

After adding the redirect, any requests for the old URL will be permanently redirected to the new URL using a 301 redirect. It’s important to note that you can also use the “Redirection” plugin to create other types of redirects, such as 302 temporary redirects or 307 temporary redirects, by choosing the appropriate option from the “Type” dropdown menu.

Conclusion

Now that you’ve learned what is a 301 redirect and how to do it properly using .htaccess file in Apache, let’s review what are the key takeaways:

  1. A 301 redirect is a permanent redirect from one URL to another.
  2. It is commonly used when a website has changed its domain name or when a page has been moved to a new location.
  3. The 301 redirect tells search engines that the old URL has been permanently moved to the new URL.
  4. It passes on most of the link equity from the old URL to the new URL.
  5. This helps to ensure that any search engine rankings and link juice that the old URL had are transferred to the new URL.
  6. You can implement a 301 redirect in your website’s .htaccess file.
  7. Using 301 redirects can help to improve your website’s SEO by maintaining link equity and preventing 404 errors.
  8. It is important to test your redirects to ensure that they are working properly and not causing any issues with your website.