A 301 redirect utilizing an .htaccess file is an HTTP standing code that you just create if you need to completely redirect one URL to a different.
There are a lot of the reason why you would possibly need to implement a 301 redirect in your website.
However how do you do it? And why must you do it the .htaccess means?
We’ve received solutions.
However earlier than we go into specifics, let’s cowl the fundamentals first.
What Is a 301 Redirect?
A 301 redirect is a everlasting redirect of a URL.
When customers attempt to entry an previous URL that has been redirected, their browser mechanically sends them to the brand new URL specified within the redirect.
That is helpful for customers as a result of it ensures a seamless transition when URLs are modified or content material is moved, preserving a optimistic person expertise.
And 301 redirects are helpful for search engines like google. As a result of they assist to maintain search engine indexes up to date.
They inform search engines like google that content material has been moved and assist be certain that the visibility and authority related to the previous web page is transferred to the brand new URL.
Like this:

For extra data, learn our full information to 301 redirects.
What Is an .htaccess File?
An .htaccess file is a textual content file used to configure totally different elements of your web site.
You should utilize it to redirect URLs, modify URL construction, customise error pages, and extra.
Utilizing an .htaccess file is a reasonably simple approach to make necessary adjustments to your website.
However you must be actually cautious that you just’re enhancing the principles within the file appropriately. As a result of one mistake may cause loads of issues for customers.
When Ought to You Carry out 301 Redirects?
There are a lot of conditions when chances are you’ll need to carry out a 301 redirect, comparable to:
- You modified the URL of a web page and need to redirect the previous URL to the brand new one
- You migrated your web site to a brand new area and need to redirect all of the pages from the previous area to the brand new one
- You switched your web site from HTTP to HTTPS and need to redirect all of the HTTP requests to HTTPS
- You’ve gotten duplicate pages that compete for a similar key phrases and need to consolidate them into one web page
- You deleted a web page and need to redirect it to a different related web page or the homepage
Utilizing a 301 redirect to ensure visitors from a deleted web page finally ends up on a related web page minimizes error messages like this:

Nevertheless, there are other ways of implementing a redirect, comparable to utilizing server-side code, JavaScript, or an .htaccess file.
Every methodology has its personal benefits and downsides, relying in your wants and preferences.
However one of the crucial widespread and efficient methods of performing a 301 redirect is utilizing .htaccess recordsdata.
Utilizing .htaccess recordsdata for 301 redirects has some advantages:
- It could possibly deal with advanced redirection patterns utilizing common expressions
- It may be used for each particular person pages and full directories
- It really works for any sort of file or useful resource in your web site
Easy methods to Set Up a 301 Redirect with an .htaccess File
A 301 redirect is executed in a different way relying on the server you utilize. The .htaccess methodology is often used on Apache servers.
Easy methods to Set Up 301 Redirects with .htaccess Recordsdata in Apache
Earlier than you do something, it is advisable find the RewriteEngine, which lets you edit guidelines. You’ll find it within the mod_rewrite module in Apache.
Load it utilizing the next code:
<IfModule mod_rewrite.c>
RewriteEngine On
</IfModule>
Then you possibly can add all of your guidelines beneath “RewriteEngine On.” The foundations you add will rely upon what precisely you need to redirect.
Redirecting a Single URL
When redirecting a single URL, all you must do is add the next redirect rule underneath the “RewriteEngine On” line:
Redirect 301 /old-page/ https://www.yourdomain.com/new-page/
Ensure you exchange URLs with the precise URLs in your website.
The primary a part of the rule ought to point out the URL path, beginning with a slash. Whereas the second a part of the rule ought to point out absolutely the (or full) URL.
For those who’re a complicated person, you too can use common expressions for redirecting URLs. The RedirectMatch 301 rule helps common expressions.
Try the Apache’s official documentation for extra particulars.
Redirecting a Single Folder
When redirecting a single folder to a brand new location, you’ll use the next redirect rule:
RewriteRule ^/?weblog/(.*)$ /information/$1 [R,L]
We’ve used “weblog” and “information” for example. On this case, the whole weblog subfolder will probably be redirected to the “information” subfolder.
When implementing this redirect in your website, make sure you use the precise web site folder names.
Redirecting WWW to Non-WWW URLs with a 301 .htaccess Redirect
Perhaps you need to implement a redirect since you don’t need to use a www subdomain.
If that’s the case, you’ll must redirect to a non-www model. Right here’s the redirect rule you need to use:
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteRule (.*) https://yourdomain.com/$1 [L,R=301]
Ensure you exchange “yourdomain” along with your precise area title.
Easy methods to Set Up 301 Redirects with .htaccess Recordsdata in WordPress
To do a 301 redirect with .htaccess recordsdata in WordPress, it is advisable observe these steps:
- Entry your website recordsdata by clicking “File Supervisor” in your cPanel or FTP consumer. On this instance, we’ll be utilizing Bluehost.

- Search for the .htaccess file within the root folder of your website.

- When you find the .htaccess file, right-click on it and choose “Edit.”

- Add the redirection guidelines underneath the “RewriteEngine On” line.

After all, the redirect guidelines you utilize will rely upon what you need to redirect.
Redirecting a Single Web page
To redirect a single web page URL, you’ll want so as to add the next rule:
Redirect 301 /old-page/ https://www.yourdomain.com/new-page/
Make sure that you substitute “/old-page/” and “https://www.yourdomain.com/new-page/” with the true URLs in your website.
For the primary a part of the rule, you possibly can simply point out the URL path. However for the second half, you’ll should specify the total URL.
Redirecting an Outdated Area to New Area
To redirect an previous area title to new area title, use the next code:
RewriteCond %{HTTP_HOST} ^(?:www.)oldsite.com$ [NC]
RewriteRule ^https://newsite.com%{REQUEST_URI} [L,R=301]
Ensure that to make use of your precise previous and new domains in your redirect guidelines.
Easy methods to Establish & Repair Redirect Points
To make sure your redirects are applied appropriately and no points are current, contemplate auditing your web site with Semrush’s Website Audit instrument.
The instrument can rapidly spot any issues your website might need with redirects. Like redirect chains and loops. So you possibly can repair them as quickly as potential. First, open the Website Audit instrument.
Enter your area title and click on “Begin Audit.”

Then the “Website Audit Settings” window will pop up.

From right here, configure the essential audit settings. Then, click on “Begin Website Audit.”
As soon as your audit is full, head over to the “Points” tab and seek for “redirect.” The instrument will present whether or not your website has any redirect-related points.

You possibly can click on “X redirect chains and loops” to see the precise URLs inflicting points.
Then, click on on the “Why and find out how to repair it” hyperlink to discover ways to resolve the difficulty.

Keep on Prime of Redirect Points
For those who implement 301 redirects utilizing an .htaccess file fairly commonly, it’s finest to schedule periodic audits within the Website Audit instrument.
It will provide you with a warning of any new points which may crop up in future as you implement extra redirects in your website.
From the Website Audit “Overview” tab, click on the cog icon situated within the right-hand nook.
Then scroll right down to the audit settings and click on “Schedule.”

You possibly can schedule each day or weekly audits. Select the choice that most accurately fits your wants and preferences. And click on “Save.”

Now the instrument will commonly scan your web site for redirect points. Repair them as quickly as they come up.