How to do 301 redirect in .htaccess

05-06-2024 - General, Guides, htaccess

An introduction on How to do 301 redirect in .htaccess

Redirecting URLs is an important and crucial part of owning or developing a website – whether you’re moving to a new domain, restructuring your site, or just fixing broken links, knowing how to set up redirects can save you and your visitors a whole lot of pain, and also help you retain your SEO rankings – even if you’ve switched domain names.

Imagine you started an online fashion store with the domain “,” but as your business grew to include pants and other accessories, you realized the name no longer fit. You decided to switch to a more fitting domain, “” Now, you need an effective way to ensure that old customers that only knew your store by “”, has a way to reach your new website. While you’ve informed them via email, not everyone checks their inbox frequently, and some notifications might land in spam. In this guide, we will explore using the .htaccess file to seamlessly do 301 redirects.

A Quick Overview of the .htaccess file

The .htaccess file is a powerful configuration tool used on web servers running Apache Web Server software. It enables you to make site-specific configuration changes on a per-directory basis, meaning each folder or subfolder on your website can have its own .htaccess file with unique settings. This flexibility allows for a multitude of adjustments, such as URL redirection, enabling password protection for specific areas of your site, and even blocking certain users or IP addresses.

In this guide, our primary focus will be on URL redirection, a critical feature for managing changes in your website’s structure and ensuring visitors can seamlessly access the correct content. Redirection is especially useful when migrating to a new domain or reorganizing your site’s content.

For those who want to dive deeper into the capabilities of the .htaccess file, we have an in-depth blog post that explores its myriad uses and configurations of the .htaccess file in detail.

How to do 301 redirect

Why Use Redirects?

User Experience

When your users unsuspectingly visit your old domain/site they get automatically sent to the correct page, avoiding the dreaded 404 error which helps to improve overall user experience and retain your viewer base.

SEO Preservation

Redirects ensure that search engines transfer the ranking power from old URLs to new ones.

Updating Site Structure

When you change your site’s structure, redirects help maintain traffic to your new URLs.

Setting Up a 301 Redirect

A 301 redirect is the most common type of redirect it’s used for when permanently moving a URL. It tells your web browser that a resource or webpage has been moved from its previous known location to a new one using request headers, so when your visitors try to access a page from the old URL, your server – being informed by the htaccess file – sends their browser a response with some meta info indicating that the page they’re trying to access has been moved to a new URL using the 302 response code – which triggers the redirect to the new URL, and then the browser caches that response so it knows that the page/resource has been permanently moved and can no longer be accessed from the old URL.

Step 1 – Access Your Server

Firstly, you need access to your website’s server files. Typically, you can do this via FTP (File Transfer Protocol) or through your web hosting control panel.

Access your webserver

Create or Edit .htaccess File 1-1
Create or Edit .htaccess File 1-2

Step 2 – Create or Edit .htaccess File

In the root directory or the /public_html folder of your website, look for a file named `.htaccess`. If you don’t find one, you can create a new text file and name it `.htaccess`.

Step 3 – Add the 301 redirect rule to your file

There isn’t just a single way to create 301 redirect rules; they can be tailored to fit your specific needs. Whether you’re redirecting individual pages, entire directories, or even cross-domain links, .htaccess offers flexible options to suit various scenarios and requirements. Below we made a few example of 301 redirect rules that can be usefull. To use them, they just need to be added to the top of your .htaccess file

a) Redirect 1 specific page

The following will redirect a single page to your choosen URL

Redirect 301 /old-page.html

b) Redirect whole directory

The following will redirect all files within `/old-directory` to your choosen URL

Redirect 301 /old-directory

c) Redirect whole domain

The following will redirect anything on your domain to the choosen URL

Redirect 301 /

d) Wildcard Redirect

You can also setup wildcard redirects if you want to redirect multiple URLs that follow a pattern. For example, to redirect all `.html` pages to `.php` pages

RedirectMatch 301 (.*)\.html$$1.php
Step 3 - Single Page 301 Redirect
Step 3 - Directory 301 Redirect
Step 3 - Full Site 301 Redirect
Step 3 - Wildcard 301 Redirect