What is .htaccess redirect?

Redirects can be used to forward both search engines and website traffic from one page to another. The following guide will help you handle redirects efficiently using .htaccess.

Updated: 01 Jun, 23 by Susith Nonis 6 Min

List of content you will read in this article:

Redirects can be used to forward both search engines and website traffic from one page to another. Redirects have an impact on a wide range of website elements, including crawlability, user experience, indexability, server load, and site speed, to mention a few.

There are different types of redirects and instances at which they are used. You can redirect your site's content through the .htaccess file. It is a powerful configuration file that can be used to configure internal URL rewrites, external redirects, and even restrict access to certain pages.

There are several instances where you can use redirects, and they include:

  1. When you want to merge websites
  2. You can use redirects to delete a page that has backlinks pointing to it or traffic. This will allow you to maintain the traffic and links, thus protecting your SEO.
  3. When the structure of your website changes during redevelopment.
  4. When migrating from HTTP to HTTPS in case you are looking to enhance the security of your website.
  5. When moving from your website to a new domain name and the URL from a page.
  6. When you are looking to prevent duplicate content on non-www URLs and trailing-slash URLs.

Provided you redirect the right way, they cannot hurt your SEO. Users are led to another page without even noticing when you set up redirects.

There are two main types of redirects: 301 and 302 redirects.

301 redirects are permanent redirects used when you redirect to the final page or version that you wish the search engine to see. In essence, permanent redirects are taken into account by search engines.

302 directs, on the other hand, are temporary and are commonly used by web admins when running promotions and when temporarily hosting a page elsewhere. With 302 redirects, the aim is to have the main page or site still indexed by the search engines. A web owner will do 302 redirects when they want to maintain the original destination and the search engines to crawl the original pages, which are often authority.

Redirects are one of the ways you can maintain your website's health. as websites are constantly changing, you may be required to restructure them, change domains and even remove some content. In order for your website to maintain its good performance through all these changes, you must know how to do redirects.  

One of the ways to create redirects is by creating redirect rules in the .htaccess file.

When your website is running on an Apache Web Server, .htaccess configuration files can allow you to make configurations to run redirects.

One of the benefits of using .htaccess for redirects is that the configuration files act on an individual basis, and you can therefore create rules that apply only to a specific directory.

To use the configuration files for redirects, you must first enable the .htaccess on your server. Using a WordPress website, you are in luck as the system automatically creates .htaccess. If it is not available, you can create it manually.

Provided you understand the syntax, using .htaccess is pretty simple. The directive should include the directive's name, in this case, Redirect, the HTTP status code, whether it is a 301 or 302 redirect, the page's original location, and the target page's location.

Essentially, the directive will take a format similar to the example below.

Redirect301/subdirectory/products.html/directory/products.html.

When creating the directive, it is critical to remain case sensitive; the "R" in the directive must be capitalized. Also, note that you must specify the status code of the redirect, lest the system will conclude that the status code is 302.

There are several ways to create the redirect directive, depending on what you want to redirect.

Here are some common redirect rules and how they are written.

  1. The directive for a redirect between pages within the same domain is Redirect 301 /subfolder/old_page.html /subfolder/new_page.html
  2. The directive for a redirect from one page to another page on a different domain is Redirect 301 /subfolder/old_page.html https://www.exampleurl.com/subfolder/new_page.html
  3. A directive for subfolders within the same domain is Redirect 301 /subfolder/old_page.html https://www.exampleurl.com/subfolder.
  4. A redirect of a subfolder to an external location is directed as Redirect 301 /subfolderhttps://www.exampleurl.com/
  5. The directive for a redirect to a custom 404 page is presented as ErrorDocument 404 /error/pagenotfound.ht

When doing .htaccess redirects, you will be doing 301 directs most of the time. Here are some key things you need to know.

  1. A page will not do a 301 redirect to itself. Trying a 301 redirect on a page to itself will create a webpage ping-pong, which essentially means nothing will happen because it creates an infinite loop.
  2. The execution of 301 redirects is often very quick. This means that you can run other tasks on your website as it does not significantly impact your site speed. More so, the user experience will not be affected.
  3. You can redirect multiple URLs to one URL; however, you cannot redirect one URL to several URLs.

Redirects can be leveraged to enhance the overall experience of your website. You can run redirects with .htaccess files whether you are doing it to delete a page on your website, after redevelopment, when transferring your site to a new domain name, and merging websites.

If not done right, redirects can affect the performance of your websites, especially if link equity is lost.

The best redirects practices that will maintain your website include avoiding chain redirects. It is best not to redirect URLs that have already been redirected. Ensuring that the redirects are set up on the server-side is also key.

Remember that different search engines handle the redirects in diverse ways. Using page numbers and nodes during redirects will lead to broken links; therefore, stick to absolute URLs. When redirecting a removed page, move it to one that is most similar. The guide above will help you handle redirects efficiently using .htaccess.

People also read:

Susith Nonis

Susith Nonis

I'm fascinated by the IT world and how the 1's and 0's work. While I venture into the world of Technology, I try to share what I know in the simplest way with you. Not a fan of coffee, a travel addict, and a self-accredited 'master chef'.